是否有可能得到一个1-100之间的随机数,并将结果主要保持在40-60的范围内?我的意思是,它很少会超出这个范围,但我希望它主要在这个范围内……这是可能的JavaScript/jQuery?

现在我只使用基本的Math.random() * 100 + 1。


当前回答

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);
}

其他回答

您可以编写一个函数,根据权重将[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>

这是3/4的加权解,40-60和1/4在这个范围之外。

function weighted() { var w = 4; // number 1 to w var r = Math.floor(Math.random() * w) + 1; if (r === 1) { // 1/w goes to outside 40-60 var n = Math.floor(Math.random() * 80) + 1; if (n >= 40 && n <= 60) n += 40; return n } // w-1/w goes to 40-60 range. return Math.floor(Math.random() * 21) + 40; } function test() { var counts = []; for (var i = 0; i < 2000; i++) { var n = weighted(); if (!counts[n]) counts[n] = 0; counts[n] ++; } var output = document.getElementById('output'); var o = ""; for (var i = 1; i <= 100; i++) { o += i + " - " + (counts[i] | 0) + "\n"; } output.innerHTML = o; } test(); <pre id="output"></pre>

我可能会做一些事情,比如设置一个“机会”,让数字被允许“出界”。在这个例子中,数字为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/

这个答案真的很好。但是我想针对不同的情况发布实现说明(我不懂JavaScript,所以我希望你能理解)。


假设每个范围都有范围和权重:

ranges - [1, 20], [21, 40], [41, 60], [61, 100]
weights - {1, 2, 100, 5}

初始静态信息,可以缓存:

所有权重之和(样本为108) 范围选择边界。基本上就是这个公式:Boundary[n] = Boundary[n - 1] + weight [n - 1] and Boundary[0] = 0。样本的边界为{0,1,3,103,108}

一代数量:

从范围[0,所有权重之和]生成随机数N。 For (i = 0;i < size(border) && N > border [i + 1];+ + i) 取第i个范围,生成该范围内的随机数。


性能优化的附加说明。范围不需要按升序或降序排列,所以为了更快的范围查找,权重最高的范围应该排在前面,权重最低的范围应该排在最后。

好的,所以我决定添加另一个答案,因为我觉得我的最后一个答案,以及这里的大多数答案,使用某种半统计的方式来获得钟形曲线类型的结果回报。下面我提供的代码与掷骰子的工作方式相同。因此,得到1或99是最难的,但得到50是最容易的。

var loops = 10; //Number of numbers generated var min = 1, max = 50; var div = $("#results").html(random()); function random() { var values = ""; for (var i = 0; i < loops; i++) { var one = generate(); var two = generate(); var ans = one + two - 1; var num = values += ans + "<br/>"; } return values; } function generate() { 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> <div id="results"></div>