HP Forums
Which formula does randNorm use? - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: Which formula does randNorm use? (/thread-9403.html)

Which formula does randNorm use? - Joe Horn - 10-30-2017 08:38 PM

According to Wikipedia, the "Box-Muller method" for generating random values which are "normally" distributed (that is, fitting the "normal" curve, AKA the bell curve) is this: generate two uniformly random numbers between 0 and 1 (e.g. two outputs from Prime's RANDOM function), call them \(U\) and \(V\), then plug them into this formula:

\({\sqrt {-2\ln U}}\cdot\cos(2\pi V)\)

This returns random numbers which are normally distributed around 0 with a standard deviation of 1. Plot a bunch of them and you'll see a nice bell curve.

Of course, if you need normally distributed random numbers centered around a mean of \(M\) with a standard deviation of \(S\), you take the above result and simply stretch it by a factor of \(S\), then shift it up by \(M\), like this:

\(M+S\cdot{\sqrt {-2\ln U}}\cdot\cos(2\pi V)\)

However, I'm puzzled why the "Box-Muller method" uses \(2\pi\) instead of just \(\pi\). Since the range of \(\cos(2\pi V)\) is the same as the range of \(\cos(\pi V)\), it seems to me that multiplying by 2 here is mathematically redundant.

The reason I'm even bringing this up is that all the programs (for older HP calculator models) which input \(M\) and \(S\) and return \(randNorm(M,S)\) do NOT multiply by 2. They simply use this formula (notice it's just \(\pi\), not \(2\pi\)):

\(M+S\cdot{\sqrt {-2\ln U}}\cdot\cos(\pi V)\)

Which leads to these questions:

1. Am I right that multiplying by 2 is unnecessary and may be omitted?
   1a. If Yes, then why does everybody say that the Box-Muller method needs \(2\pi\)?
2. What formula does the randNorm function in the HP Prime use?
   2a. If it uses Box-Muller with \(2\pi\), why is the 2 in there?

RE: Which formula does randNorm use? - parisse - 10-30-2017 09:11 PM

The CAS code for randNorm is

    double u=giac_rand(contextptr)/(rand_max2+1.0);
    double d=giac_rand(contextptr)/(rand_max2+1.0);
    return std::sqrt(-2*std::log(u))*std::cos(2*M_PI*d);
Frankly speaking I used the formula without taking care of why it works, it's not really CAS...

RE: Which formula does randNorm use? - Joe Horn - 10-30-2017 11:10 PM

Thanks for answering questions 2 and 2a, Bernard! If any of y'all can answer questions 1 and 1a, I'd be most grateful.