是否有可能得到一个1-100之间的随机数,并将结果主要保持在40-60的范围内?我的意思是,它很少会超出这个范围,但我希望它主要在这个范围内……这是可能的JavaScript/jQuery?
现在我只使用基本的Math.random() * 100 + 1。
是否有可能得到一个1-100之间的随机数,并将结果主要保持在40-60的范围内?我的意思是,它很少会超出这个范围,但我希望它主要在这个范围内……这是可能的JavaScript/jQuery?
现在我只使用基本的Math.random() * 100 + 1。
当前回答
最好的方法是生成一个随机数,该随机数平均分布在某一组数字中,然后对0到100之间的集合应用投影函数,其中投影更有可能击中你想要的数字。
通常,实现这一点的数学方法是绘制所需数字的概率函数。我们可以用钟形曲线,但为了计算方便,我们还是用翻转抛物线吧。
我们画一条抛物线,使它的根在0和100处,而不使它倾斜。得到如下方程:
f(x) = -(x-0)(x-100) = -x * (x-100) = -x^2 + 100x
现在,曲线下0到100之间的所有面积都代表了我们想要生成数字的第一个集合。在那里,生成是完全随机的。我们要做的就是求出第一个集合的上界。
下界当然是0。上限是函数在100处的积分,也就是
F(x) = -x^3/3 + 50x^2
F(100) = 500,000/3 = 166,666.66666 (let's just use 166,666, because rounding up would make the target out of bounds)
所以我们知道我们需要生成一个介于0到166666之间的数字。然后,我们只需要把这个数字投影到我们的第二个集合,它在0到100之间。
我们知道我们生成的随机数是输入x在0到100之间的抛物线的积分。这意味着我们只需假设随机数是F(x)的结果,然后解出x。
在这种情况下,F(x)是一个三次方程,形式为F(x) = ax^3 + bx^2 + cx + d = 0,下列表述成立:
a = -1/3
b = 50
c = 0
d = -1 * (your random number)
为x解出这个问题会得到你所寻找的实际随机数,它保证在[0,100]范围内,并且更接近中心而不是边缘。
其他回答
我可能会做一些事情,比如设置一个“机会”,让数字被允许“出界”。在这个例子中,数字为1-100的概率为20%,否则为40-60:
$(function () { $('button').click(function () { var outOfBoundsChance = .2; var num = 0; if (Math.random() <= outOfBoundsChance) { num = getRandomInt(1, 100); } else { num = getRandomInt(40, 60); } $('#out').text(num); }); function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <button>Generate</button> <div id="out"></div>
小提琴:http://jsfiddle.net/kbv39s9w/
它看起来很蠢,但你可以使用两次rand:
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;
}
你可以使用一个辅助随机数来决定是否生成40-60或1-100的随机数:
// 90%的随机数应该在40到60之间。 Var weight_percentage = 90; var focuse_on_center = ((Math.random() * 100) < weight_percentage); 如果(focuse_on_center) { //生成40-60范围内的随机数。 alert (40 + Math.random() * 20 + 1); } 其他的 { //生成1-100范围内的随机数。 alert (Math.random() * 100 + 1); }
您可以编写一个函数,根据权重将[0,1)到[1,100]之间的随机值映射。想想这个例子:
这里,值0.95映射到[61,100]之间的值。 事实上,我们有。05 / .1 = 0.5,当映射到[61,100]时,结果是81。
函数如下:
/* * Function that returns a function that maps random number to value according to map of probability */ function createDistributionFunction(data) { // cache data + some pre-calculations var cache = []; var i; for (i = 0; i < data.length; i++) { cache[i] = {}; cache[i].valueMin = data[i].values[0]; cache[i].valueMax = data[i].values[1]; cache[i].rangeMin = i === 0 ? 0 : cache[i - 1].rangeMax; cache[i].rangeMax = cache[i].rangeMin + data[i].weight; } return function(random) { var value; for (i = 0; i < cache.length; i++) { // this maps random number to the bracket and the value inside that bracket if (cache[i].rangeMin <= random && random < cache[i].rangeMax) { value = (random - cache[i].rangeMin) / (cache[i].rangeMax - cache[i].rangeMin); value *= cache[i].valueMax - cache[i].valueMin + 1; value += cache[i].valueMin; return Math.floor(value); } } }; } /* * Example usage */ var distributionFunction = createDistributionFunction([ { weight: 0.1, values: [1, 40] }, { weight: 0.8, values: [41, 60] }, { weight: 0.1, values: [61, 100] } ]); /* * Test the example and draw results using Google charts API */ function testAndDrawResult() { var counts = []; var i; var value; // run the function in a loop and count the number of occurrences of each value for (i = 0; i < 10000; i++) { value = distributionFunction(Math.random()); counts[value] = (counts[value] || 0) + 1; } // convert results to datatable and display var data = new google.visualization.DataTable(); data.addColumn("number", "Value"); data.addColumn("number", "Count"); for (value = 0; value < counts.length; value++) { if (counts[value] !== undefined) { data.addRow([value, counts[value]]); } } var chart = new google.visualization.ColumnChart(document.getElementById("chart")); chart.draw(data); } google.load("visualization", "1", { packages: ["corechart"] }); google.setOnLoadCallback(testAndDrawResult); <script src="https://www.google.com/jsapi"></script> <div id="chart"></div>
分布
5% for [ 0,39]
90% for [40,59]
5% for [60,99]
解决方案
var f = Math.random();
if (f < 0.05) return random(0,39);
else if (f < 0.95) return random(40,59);
else return random(60,99);
通用解决方案
random_choose([series(0,39),series(40,59),series(60,99)],[0.05,0.90,0.05]);
function random_choose (collections,probabilities)
{
var acc = 0.00;
var r1 = Math.random();
var r2 = Math.random();
for (var i = 0; i < probabilities.length; i++)
{
acc += probabilities[i];
if (r1 < acc)
return collections[i][Math.floor(r2*collections[i].length)];
}
return (-1);
}
function series(min,max)
{
var i = min; var s = [];
while (s[s.length-1] < max) s[s.length]=i++;
return s;
}