Get a random number focused on center

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

    I'd recommend using the beta distribution to generate a number between 0-1, then scale it up. It's quite flexible and can create many different shapes of distributions.

    Here's a quick and dirty sampler:

    rbeta = function(alpha, beta) {
     var a = 0   
     for(var i = 0; i < alpha; i++)   
        a -= Math.log(Math.random())
    
     var b = 0   
     for(var i = 0; i < beta; i++)   
        b -= Math.log(Math.random())
    
      return Math.ceil(100 * a / (a+b))
    }
    

提交回复
热议问题