亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

javascript - How to get a random number in a symmetric range in JS?
淡淡煙草味
淡淡煙草味 2017-07-05 10:44:41
0
4
938

For example, I want to use Math.random() to get random numbers in the ranges of -20~-10 and 10~20. Is there any most streamlined solution? For example, can it be done without an if statement? Thank you all for clearing up the confusion.

淡淡煙草味
淡淡煙草味

reply all(4)
phpcn_u1582
(Math.floor(Math.random() * (20 - 10 + 1)) + 10) * (Math.random() < 0.5 ? -1 : 1)
某草草
function getRandom(min,max){
    return Math.random()*(max-min)+min;
}
為情所困
Math.random() * (max - min) + min;

See details


Modification:

(Math.random() * (max - min) + min)*(Math.random()<0.5?1:-1);
學霸
function rand(min, max) {
    if ( min >= max ) {
        return;
    }
    return  Math.floor(min + (max - min+1) * Math.random());
}

This is to take an integer within a range

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template