This method is based on the following 3 steps algorithm :
1 - generate two uniform numbers on the [-1,1] interval that you will call U1 and U2
2 - calculate S = U1 ^2 + U2^2
3 - If S < 1 the normal number is given by U1 * square root (-2 ln (S)/S) otherwise go back to step 1 until S < 1.
Program this function in VB and give it the name BoxMuller.
This is the function I wrote based on above steps I am not sure whether it's correct or not because sometimes it returns #Value error
I pass following values to the function =BoxMuller(Rand(),Rand())
Function BoxMuller(U1 As Double, U2 As Double) As Double
Dim S As Double
Do
U1 = WorksheetFunction.NormInv(U1, 0, 1)
U2 = WorksheetFunction.NormInv(U2, 0, 1)
S = U1 * U1 + U2 * U2
If S < 1 Then
BoxMuller = U1 * Sqr(-2 * Log(S) / S)
Exit Function
End If
Loop Until S < 1
End Function
is the Loop Until S < 1 condition right because I think that maybe the real cause of the error.
Also tried the following :
Function BoxMuller() As Double
Dim S As Double
Dim U1 As Double
Dim U2 As Double
Do
U1 = WorksheetFunction.RandBetween(-1, 1)
U2 = WorksheetFunction.RandBetween(-1, 1)
S = U1 * U1 + U2 * U2
If S < 1 Then
BoxMuller = U1 * Sqr(-2 * Log(S) / S)
Exit Function
End If
Loop
End Function
And Called =BoxMuller() Still #Value Error