Number of normals N = 619x
(HTML5 canvas support required)
(HTML5 canvas support required)
JavaScript has only provided a uniform random variable Math.random() and a normal random number is sometimes a necessary one to have. There is a polar method to simulate normal RV from uniform RV in A First Course of Probability by Sheldon Ross (pp. 464, 6ed):
- Generate random numbers and .
- Set .
- If , return step 1.
- Return the independent unit normals .
Here is the (almost) equivalent code in JavaScript:
function normal_random(mean, variance) { if (mean == undefined) mean = 0.0; if (variance == undefined) variance = 1.0; var V1, V2, S; do { var U1 = Math.random(); var U2 = Math.random(); V1 = 2 * U1 - 1; V2 = 2 * U2 - 1; S = V1 * V1 + V2 * V2; } while (S > 1); X = Math.sqrt(-2 * Math.log(S) / S) * V1; //Y = Math.sqrt(-2 * Math.log(S) / S) * V2; X = mean + Math.sqrt(variance) * X; //Y = mean + Math.sqrt(variance) * Y ; return X; }
The following figure is :
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.