Get a random number focused on center

后端 未结 20 3017
离开以前
离开以前 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:35

    It looks stupid but you can use rand twice:

    var choice = Math.random() * 3;
    var result;
    
    if (choice < 2){
        result = Math.random() * 20 + 40; //you have 2/3 chance to go there
    }
    else {
        result = Math.random() * 100 + 1;
    }
    

提交回复
热议问题