Get a random number focused on center

后端 未结 20 3018
离开以前
离开以前 2020-12-12 09:28

Is it possible to get a random number between 1-100 and keep the results mainly within the 40-60 range? I mean, it will go out of that range rarely, but I want it to be main

20条回答
  •  爱一瞬间的悲伤
    2020-12-12 09:37

    var randNum;
    // generate random number from 1-5
    var freq = Math.floor(Math.random() * (6 - 1) + 1);
    // focus on 40-60 if the number is odd (1,3, or 5)
    // this should happen %60 of the time
    if (freq % 2){
        randNum = Math.floor(Math.random() * (60 - 40) + 40);
    }
    else {
        randNum = Math.floor(Math.random() * (100 - 1) + 1);
    }
    

提交回复
热议问题