获取集中在中心的随机数 [英] Get a random number focused on center
问题描述
时有可能得到1-100之间的随机数,并保持主要在40-60范围之内的结果?我的意思是,它会走出去的范围很少,但我想这主要是该范围内?是否有可能使用JavaScript / jQuery的?
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 mainly within that range... Is it possible with JavaScript/jQuery?
现在我只使用基本的的Math.random()* 100 + 1
。
Right now I'm just using the basic Math.random() * 100 + 1
.
推荐答案
的最简单的方法是从0-50产生两个随机数,并将其添加在一起。
The simplest way would be to generate two random numbers from 0-50 and add them together.
此使偏向50的分布,以相同的方式滚动两个骰子偏压朝向7
This gives a distribution biased towards 50, in the same way rolling two dice biases towards 7.
通过使用不同的设置( bellFactor
)的@Falco建议,你可以很容易地从均匀分布和三角形分布或不同的钟形曲线选择
By using different settings (bellFactor
) as @Falco suggests, you can easily choose from even distribution or triangle distribution or different bell curves:
function weightedRandom(max, bellFactor) {
var num = 0;
for (var i = 0; i < bellFactor; i++) {
num += Math.random() * (max/bellFactor);
}
return num;
}
的jsfiddle: http://jsfiddle.net/797qhcza/1/
JSFiddle: http://jsfiddle.net/797qhcza/1/
这篇关于获取集中在中心的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!