Search code examples
excelvba

Use of rnd() function in VBA


I have a custom defined function as below

Function abc(Seed As Long)
Static iset As Integer
Static gset As Double
MsgBox Rnd(Seed)
MsgBox Rnd(Seed)
abc = 0
End Function

However I get two different values of Rnd(Seed) from the MsgBox. However since I fixed the seed, I was expecting I would get same values.

Could you please help me to understand how the seed works in rnd()


Solution

  • This is from Excel VBA help:

    Returns a Single containing a random number.

    Syntax

    Rnd[(number)]

    The optional number argument is a Single or any valid numeric expression.

    Return Values

    If number is Rnd generates
    Greater than zero The next random number in the sequence.
    Not supplied The next random number in the sequence.
    Less than zero The same number every time, using number as the seed.
    Equal to zero The most recently generated number.

    According to this you need to apply the bolded text.