Search code examples
excelrandomexcel-2013montecarlovba

Is Excel VBA's Rnd() really this bad?


I need a pseudo random number generator for 2D Monte Carlo simulation that doesn't have the characteristic hyperplanes that you get with simple LCGs. I tested the random number generator Rnd() in Excel 2013 using the following code (takes about 5 secs to run):

Sub ZoomRNG()

Randomize
For i = 1 To 1000
    Found = False
    Do
        x = Rnd()   ' 2 random numbers between 0.0 and 1.0
        y = Rnd()
        If ((x > 0.5) And (x < 0.51)) Then
            If ((y > 0.5) And (y < 0.51)) Then
                ' Write if both x & y in a narrow range
                Cells(i, 1) = i
                Cells(i, 2) = x
                Cells(i, 3) = y
                Found = True
            End If
        End If
    Loop While (Not Found)
Next i

End Sub

Here is a simple plot of x vs y from running the above code

enter image description here

Not only is it not very random-looking, it has more obvious hyperplanes than the infamous RANDU algorithm does in 2D. Basically, am I using the function incorrectly or is the Rnd() function in VBA actually not the least bit usable?

For comparison, here's what I get for the Mersenne Twister MT19937 in C++.

enter image description here


Solution

  • To yield a better random generator and to make its performance faster, I modified your code like this:

    Const N = 1000           'Put this on top of your code module
    Sub ZoomRNG()
    
    Dim RandXY(1 To N, 1 To 3) As Single, i As Single, x As Single, y As Single
    
    For i = 1 To N
        Randomize            'Put this in the loop to generate a better random numbers
        Do
            x = Rnd
            y = Rnd
            If x > 0.5 And x < 0.51 Then
                If y > 0.5 And y < 0.51 Then
                    RandXY(i, 1) = i
                    RandXY(i, 2) = x
                    RandXY(i, 3) = y
                    Exit Do
                End If
            End If
        Loop
    Next
    Cells(1, 9).Resize(N, 3) = RandXY
    End Sub
    

    I obtain this after plotting the result

    enter image description here

    The result looks better than your code's output. Modifying the above code a little bit to something like this

    Const N = 1000
    Sub ZoomRNG()
    
    Dim RandXY(1 To N, 1 To 3) As Single, i As Single, x As Single, y As Single
    
    For i = 1 To N
        Randomize
        Do
            x = Rnd
            If x > 0.5 And x < 0.51 Then
                y = Rnd
                If y > 0.5 And y < 0.51 Then
                    RandXY(i, 1) = i
                    RandXY(i, 2) = x
                    RandXY(i, 3) = y
                    Exit Do
                End If
            End If
        Loop
    Next
    Cells(1, 9).Resize(N, 3) = RandXY
    End Sub
    

    yields a better result than the previous one

    enter image description here

    Sure the Mersenne Twister MT19937 in C++ is still better, but the last result is quite good for conducting Monte-Carlo simulations. FWIW, you might be interested in reading this paper: On the accuracy of statistical procedures in Microsoft Excel 2010.