Thread: random number
View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Myrna Larson Myrna Larson is offline
external usenet poster
 
Posts: 863
Default random number

Yeah, I wrote that before Mike Middleton (I think it was) pointed out the
simple worksheet formula using NORMINV and RAND(). Of course there are those
bugs in NORMINV that may cause problems.

On Sun, 20 Feb 2005 15:50:53 -0500, "Tom Ogilvy" wrote:

What distribution - Normal?

Myrna Larson:


Function RandomNormal(Optional Mean As Double = 0, _
Optional SD As Double = 1) As Double
'This function is to enable to use normal distrubtion vs the Rnd
' function which is uniform
'This function can be called if a uniform distrubution is not
' warranted
Static HaveX1 As Boolean
Dim V1 As Double, V2 As Double, S As Double
Static X1 As Double
Dim X2 As Double

If HaveX1 = False Then
Do
V1 = Rnd() * 2 - 1
V2 = Rnd() * 2 - 1
S = V1 * V1 + V2 * V2
Loop While S = 1# Or S = 0#

S = Sqr(-2 * Log(S) / S)
X1 = V1 * S
X2 = V2 * S

HaveX1 = True
RandomNormal = X2 * SD + Mean
Else
HaveX1 = False
RandomNormal = X1 * SD + Mean
End If
End Function 'RandomNormal