vendredi 26 février 2016

#VALUE Error for the function random number and normal random number

Based on the Rand Excel formula I tried to generate one normal number in the following 12 cells.

I used following same formula in 12 different cells =NORM.INV(RAND(),0,1) Is this correct ? if yes what is different between normal number and random number if there is ant ?

Now I have implemented new method to generate normal random number. 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.

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

But the above function sometimes return #VALUE Error When I use following formula =BoxMuller(RAND(),RAND()) . Where am I wrong ?




Aucun commentaire:

Enregistrer un commentaire