Generating Uniform Distribution Using Math.random()
In the MDN Math.random web page the comment for the example function getRandomInt(..) says that Math.round() was not used since it gives non-uniform distribution, which implies usi
Solution 1:
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 initializedata[a] = 1;
} else {
data[a] = data[a] + 1;
}
http://jsfiddle.net/hd9tt509/1/
Post a Comment for "Generating Uniform Distribution Using Math.random()"