Random integer in a certain range excluding one number

后端 未结 7 2299
广开言路
广开言路 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: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.

提交回复
热议问题