This is a very specific question mixing up stochastic knowledge and VBA skills. So very exciting!
I'm trying to compare several methods for generating standard, normally distributed numbers given a source of uniformly distributed random numbers. Therefore I'm implementing the Box Muller Algorithm, Ziggurat Algorithm and Ratio of Uniforms Algorithm. Every single implementation works great in terms of generating a clean standard, normally distribution. (checked by Shapiro-Wilk-Test).
What I want to find out: which is the quickest method?
Testing every single program with a total of 10^7 generated numbers these are the run times:
Box-Muller: 3,7 seconds
Ziggurat: 1,28 seconds
Ratio of Uniforms: 10,77 seconds
Actually I am very happy about those readings, because I didn't expect it to be that fast. Of course the run time of every single method also depends on my programming skills and VBA knowledge.
My problem: after doing some research I found out that the Ratio of Uniforms Algorithm should be the quickest (about 3 to 4 times quicker than Box Muller). This information just leans on this stack:
I am curious if this is just a wrong claim of this user or (what I do expect more) if my code is not perfectly implemented. Therefore I'll post my code and hope someone could help me with my question, if my code is just not good enough or if the Ratio of Uniforms just doesn't work that quick as mentioned.
Sub RatioUniforms() Dim x(10000000) As Double Dim passing As Long Dim amount As Long: amount = 10000000 Dim u1 As Double Dim u2 As Double Dim v2 As Double Do While passing <= amount Do u1 = Rnd 'rnd= random number(0,1) Loop Until u1 <> 0 'u1 musn't become 0 v2 = Rnd u2 = (2 * v2 - 1) * (2 * exp(-1)) ^ (1 / 2) If u1 ^ 2 <= exp(-1 / 2 * u2 ^ 2 / u1 ^ 2) Then x(passing) = u2 / u1 passing = passing + 1 End If Loop End Sub
Thank you very much helping me on this topic. Maybe some of you have tried those algorithms in VBA or other languages and can help me with there experience about the run time? If you need something else to know about my other implementations just let me know. Have a great day!