最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Generating uniform distribution using Math.random() - Stack Overflow

programmeradmin2浏览0评论

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 badges
Add a ment  | 

1 Answer 1

Reset to default 5

You 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>

发布评论

评论列表(0)

  1. 暂无评论