Number of normals

(HTML5 canvas support required)

*N*= 619x(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 ${U}_{1}$ and ${U}_{2}$.
- Set ${V}_{1}=2{U}_{1}-1,{V}_{2}=2{U}_{2}-1,S={V}_{1}^{2}+{V}_{2}^{2}$.
- If $S<1$, return step 1.
- Return the independent unit normals $X=\sqrt{\frac{-2logS}{S}}{V}_{1},Y=\sqrt{\frac{-2logS}{S}}{V}_{2}$.

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 $N=619100000$:

## 0 comments:

## Post a Comment