I’m new to programming, so I apologize in advance if I’m phrasing this question naively. Anyway, I’m trying to implement the following code (see below) that I found online for two functions to generate a pseudorandom Gaussian-distributed number. But when I try to use the code, I get the following errors:
App.Normal, line 1 This item does not exist: Normal = GetGausse*Sigma+Mean
App.GetGausse, line 17 Syntax error: Const Two = 2#, One = 1#
App.GetGausse, line 20 This item does not exist: GetGausse = dblReturn2
App.GetGausse, line 22 This item does not exist: Work3 = Two
and a number of similar errors with App.GetGausse
What am I doing wrong? How can I fix it?
[code]Private Function Normal(Optional Sigma As Double = 1, Optional Mean As Double = 0) As Double
Normal = GetGausse * Sigma + Mean
End Function
Private Function GetGausse() As Double
’ This Function returns a standard Gaussian random number
’ based upon the polar form of the Box-Muller transform.
’ since this calc is capable of returning two calculations per
’ call, it’s been set up to save the second calc for the next
’ pass through the function, saving some time.
’ Call the randomize function once (and ONLY once) in the life of the project.
Static blReturn2 As Boolean ’ Flag to calc new values, or return
’ previously calculated value. It defaults
’ to False on the first pass.
Static dblReturn2 As Double ’ Second return value
Dim Work1 As Double, Work2 As Double, Work3 As Double
Const Two = 2#, One = 1#
If blReturn2 Then ’ On odd numbered calls
GetGausse = dblReturn2
Else
Work3 = Two
Do Until Work3 < One
Work1 = Two * Rnd - One
Work2 = Two * Rnd - One
Work3 = Work1 * Work1 + Work2 * Work2
Loop
Work3 = Sqr((-(Two) * Log(Work3)) / Work3)
GetGausse = Work1 * Work3
’ a second valid value will be returned by Work2 * Work3.
’ Calculate it for the next pass. This will save some processing
dblReturn2 = Work2 * Work3
End If
blReturn2 = Not blReturn2 ’ and toggle the return value flag
End Function[/code]