Random integer in a certain range excluding one number

后端 未结 7 2287
广开言路
广开言路 2020-12-03 17:49

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 some

相关标签:
7条回答
  • 2020-12-03 18:18

    You could just continue generating the number until you find it suits your needs:

    function randomExcluded(start, end, excluded) {
        var n = excluded
        while (n == excluded)
            n = Math.floor((Math.random() * (end-start+1) + start));
        return n;
    }
    
    myRandom = randomExcluded(1, 10000, 577);
    

    By the way this is not the best solution at all, look at my other answer for a better one!

    0 讨论(0)
  • 2020-12-03 18:30

    To expand on @Jesus Cuesta's answer:

    function RandomNumber(min, max, exclusions) {
        var hash = new Object();
        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.

    0 讨论(0)
  • 2020-12-03 18:33

    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));
    }   
    
    0 讨论(0)
  • 2020-12-03 18:33

    Exclude the number from calculations:

    function toggleRand() {
      // demonstration code only.
      // this algorithm does NOT produce random numbers.
      // return `0` - `576` , `578` - `n`  
      return [Math.floor((Math.random() * 576) + 1)
              ,Math.floor(Math.random() * (100000 - 578) + 1)
             ]
             // select "random" index 
             [Math.random() > .5 ? 0 : 1];
    }
    
    console.log(toggleRand());


    Alternatively, use String.prototype.replace() with RegExp /^(577)$/ to match number that should be excluded from result; replace with another random number in range [0-99] utilizing new Date().getTime(), isNaN() and String.prototype.slice()

    console.log(
      +String(Math.floor(Math.random()*(578 - 575) + 575))
      .replace(/^(577)$/,String(isNaN("$1")&&new Date().getTime()).slice(-2))
    );


    Could also use String.prototype.match() to filter results:

    console.log(
      +String(Math.floor(Math.random()*10)) 
      .replace(/^(5)$/,String(isNaN("$1")&&new Date().getTime()).match(/[^5]/g).slice(-1)[0])
    );

    0 讨论(0)
  • 2020-12-03 18:36

    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:

    function randomExcluded(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).

    0 讨论(0)
  • 2020-12-03 18:41

    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.

    0 讨论(0)
提交回复
热议问题