JavaScript generate random number except some values

后端 未结 13 1645
野性不改
野性不改 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)

提交回复
热议问题