admin管理员组文章数量:1332896
In the MDN Math.random web page the ment for the example function getRandomInt(..)
says that Math.round()
was not used since it gives non-uniform distribution, which implies using Math.floor(..) will produce uniform distribution.
// Returns a random integer between min (included) and max (excluded)
// Using Math.round() will give you a non-uniform distribution!
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
However, the following code shows that the frequency of generating a random number is directly proportional to the value of the number. i.e Higher the value of the number, higher the frequency. This behaviour is same on nodejs and on firefox browser.
//
// Returns a random integer between min (included) and max (excluded)
// Using Math.round() will give you a non-uniform distribution!
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
var data = {};
var a;
var i = 0;
for (i = 0; i < 100000; ++i) {
a = getRandomInt(1, 50);
if (typeof data[a] === 'undefined') { // first time initialize
data[a] = a;
} else {
data[a] = data[a] + a;
}
}
//console.log(data);
document.getElementById("json").innerHTML = JSON.stringify(data, undefined, 2);
<pre id="json"></pre>
In the MDN Math.random web page the ment for the example function getRandomInt(..)
says that Math.round()
was not used since it gives non-uniform distribution, which implies using Math.floor(..) will produce uniform distribution.
// Returns a random integer between min (included) and max (excluded)
// Using Math.round() will give you a non-uniform distribution!
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
However, the following code shows that the frequency of generating a random number is directly proportional to the value of the number. i.e Higher the value of the number, higher the frequency. This behaviour is same on nodejs and on firefox browser.
// https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
// Returns a random integer between min (included) and max (excluded)
// Using Math.round() will give you a non-uniform distribution!
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
var data = {};
var a;
var i = 0;
for (i = 0; i < 100000; ++i) {
a = getRandomInt(1, 50);
if (typeof data[a] === 'undefined') { // first time initialize
data[a] = a;
} else {
data[a] = data[a] + a;
}
}
//console.log(data);
document.getElementById("json").innerHTML = JSON.stringify(data, undefined, 2);
<pre id="json"></pre>
So with this property of Math.random() how to generate a uniform distribution.
Share Improve this question edited Jan 3, 2023 at 1:13 EzioMercer 2,0172 gold badges10 silver badges27 bronze badges asked Nov 7, 2014 at 6:03 Talespin_KitTalespin_Kit 21.9k34 gold badges95 silver badges140 bronze badges1 Answer
Reset to default 5You increment your counter using a
. The result of the counter would be a*<actual frequency>
.
If you increment with 1
, you will see that it actually has a uniform distribution.
if (typeof data[a] === 'undefined') { // first time initialize
data[a] = 1;
} else {
data[a] = data[a] + 1;
}
// https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
// Returns a random integer between min (included) and max (excluded)
// Using Math.round() will give you a non-uniform distribution!
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
var data = {};
var a;
var i = 0;
for (i = 0; i < 100000; ++i)
{
a = getRandomInt(1,50);
if (typeof data[a] === 'undefined') { // first time initialize
data[a] = 1;
} else {
data[a] = data[a] + 1;
}
}
//console.log(data);
document.getElementById("json").innerHTML = JSON.stringify(data, undefined, 2);
<pre id="json"></pre>
本文标签: javascriptGenerating uniform distribution using Mathrandom()Stack Overflow
版权声明:本文标题:javascript - Generating uniform distribution using Math.random() - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742315036a2451643.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论