Skip to content Skip to sidebar Skip to footer

Random Integer In A Certain Range Excluding One Number

I would like get a random number in a range excluding one number (e.g. from 1 to 1000 exclude 577). I searched for a solution, but never solved my issue. I want something like: Mat

Solution 1:

The fastest way to obtain a random integer number in a certain range [a, b], excluding one value c, is to generate it between a and b-1, and then increment it by one if it's higher than or equal to c.

Here's a working function:

functionrandomExcluded(min, max, excluded) {
    var n = Math.floor(Math.random() * (max-min) + min);
    if (n >= excluded) n++;
    return n;
}

This solution only has a complexity of O(1).

Solution 2:

One possibility is not to add 1, and if that number comes out, you assign the last possible value.

For example:

var result= Math.floor((Math.random() *100000));
if(result==577) result=100000;

In this way, you will not need to re-launch the random method, but is repeated. And meets the objective of being a random.

Solution 3:

As @ebyrob suggested, you can create a function that makes a mapping from a smaller set to the larger set with excluded values by adding 1 for each value that it is larger than or equal to:

// min - integer// max - integer// exclusions - array of integers//            - must contain unique integers between min & maxfunctionRandomNumber(min, max, exclusions) {
    // As @Fabian pointed out, sorting is necessary // We use concat to avoid mutating the original array// See: http://stackoverflow.com/questions/9592740/how-can-you-sort-an-array-without-mutating-the-original-arrayvar exclusionsSorted = exclusions.concat().sort(function(a, b) {
        return a - b
    });

    var logicalMax = max - exclusionsSorted.length;
    var randomNumber = Math.floor(Math.random() * (logicalMax - min + 1)) + min;

    for(var i = 0; i < exclusionsSorted.length; i++) {
        if (randomNumber >= exclusionsSorted[i]) {
            randomNumber++;
        }
    }

    return randomNumber;
}

Example Fiddle

Also, I think @JesusCuesta's answer provides a simpler mapping and is better.

Update: My original answer had many issues with it.

Solution 4:

To expand on @Jesus Cuesta's answer:

functionRandomNumber(min, max, exclusions) {
    var hash = newObject();
    for(var i = 0; i < exclusions.length; ++i ) {  // TODO: run only once as setup
       hash[exclusions[i]] = i + max - exclusions.length;
    }
    var randomNumber = Math.floor((Math.random() * (max - min - exclusions.length)) + min);
    if (hash.hasOwnProperty(randomNumber)) {
       randomNumber = hash[randomNumber];
    }
    return randomNumber;
}

Note: This only works if max - exclusions.length > maximum exclusion. So close.

Solution 5:

Generate a random number and if it matches the excluded number then add another random number(-20 to 20)

var max = 99999, min = 1, exclude = 577;
var num = Math.floor(Math.random() * (max - min)) + min ;
while(num == exclude || num > max || num < min ) {
    var rand = Math.random() > .5 ? -20 : 20 ;
    num += Math.floor((Math.random() * (rand));
}   

Post a Comment for "Random Integer In A Certain Range Excluding One Number"