Answer the question
In order to leave comments, you need to log in
Normal distribution of the sum of 2 dice?
You need to display two numbers on the page at the same time, from 1 to 6 each, but such that the sum of these numbers obeys the normal distribution. I must say right away that I am far from mathematics. I thought that the Box-Muller transform function could help, like:
function randn_bm() {
let u = 0, v = 0;
while(u === 0) u = Math.random();
while(v === 0) v = Math.random();
let num = Math.sqrt( -2.0 * Math.log( u ) ) * Math.cos( 2.0 * Math.PI * v );
num = num / 10.0 + 0.5;
if (num > 1 || num < 0) return randn_bm()
return num
}
Answer the question
In order to leave comments, you need to log in
So?
function randn_bm() {
let u = 0, v = 0;
while(u === 0) u = Math.random(); //Converting [0,1) to (0,1)
while(v === 0) v = Math.random();
let num = Math.sqrt( -2.0 * Math.log( u ) ) * Math.cos( 2.0 * Math.PI * v );
num = num / 10.0 + 0.5; // Translate to 0 -> 1
if (num > 1 || num < 0) return randn_bm() // resample between 0 and 1
return num
}
function randomIntegerInRange(min, max) {
return Math.floor(randn_bm() * (max - min + 1)) + min;
}
console.log(randomIntegerInRange(1, 6));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question