Random integer in a certain range excluding one number

后端 未结 7 2290
广开言路
广开言路 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!

提交回复
热议问题