JavaScript generate random number except some values

后端 未结 13 1640
野性不改
野性不改 2021-01-05 11:26

I\'m generating random numbers from 1 to 20 by calling generateRandom(). How can I exclude some values, say 8 and 15?

function generateRandom(mi         


        
相关标签:
13条回答
  • 2021-01-05 12:00

    it should be or instead of and

    function generateRandom(min, max) {
        var num = Math.floor(Math.random() * (max - min + 1)) + min;
        return (num === 8 || num === 15) ? generateRandom(min, max) : num;
    }
    
    var test = generateRandom(1, 20)

    0 讨论(0)
  • 2021-01-05 12:05

    This is a simple and neat idea, I am a electromechanical engineer and I am just learning JS. This is going to print a random numeber between 1 and 100. Except 8 and 15

     var r;   // this is the random integer.
     var val; //this will serve as validator for the random integer.
    
     val=0; 
     while(val==0)    {
     r=Math.round(Math.random()*100)+1;
     if(r!=8 && r!=15){val=1;}    //a valid number will be any number different from 8 and 15
                                  //then validator will change and go out from the loop.
                    }   
    document.write(r);
    
    0 讨论(0)
  • 2021-01-05 12:07

    Right now I'm using this and it works without causing browser issues with infinities loops, also tested in mobile devices (using Ionic/Cordova):

    function getRandomIndex(usedIndexs, maxIndex) {
        var result = 0;
        var min = 0;
        var max = maxIndex - 1;
        var index = Math.floor(Math.random()*(max-min+1)+min);
    
        while(usedIndexs.indexOf(index) > -1) {
            if (index < max) {
                index++;
            } else {
              index = 0;
            }
        }
    
        return index;
    }
    
    0 讨论(0)
  • 2021-01-05 12:07

    I think it should be like this, if you want good distribution on all numbers. and, for this solution, it is required to higher max than 15 and lower min that 8

    function generateRandom(min, max) {
        var v =  Math.floor(Math.random() * (max - min + 1 - 2)) + min;
        if (v == 8) return max-1;
        else if (v == 15) return max-2;
        else return v;
    }
    
    var test = generateRandom(1, 20)
    
    0 讨论(0)
  • 2021-01-05 12:10

    You could make use of a recursive function

    function generateRandom(min, max, num1, num2) {
        var rtn = Math.floor(Math.random() * (max - min + 1)) + min;
        return rtn == num1 || rtn == num2 ? generateRandom(min, max, num1, num2) : rtn;
    }
    
    0 讨论(0)
  • 2021-01-05 12:13

    You can build an array dynamically. Depending on where you are getting the excluded numbers. Something like:

    var excluded = [8, 15];
    var random = [];
    for(var i = min; i <= max; i++) {
      if(excluded.indexOf(i) !== -1) {
        random.push(i);
      }
    }
    

    Then use the tips found in the answer for this post: How can I generate a random number within a range but exclude some?. Should get you to where you want to go.

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