This function returns a pseudo-random number in the range 0 to 1. The optional argument is called a seed and is used as a starting point in the calculations that generate the random number.

NOTE The sequence of random numbers produced by Visual Basic is always the same! Let's say you have an application that displays three random numbers. f you stop and rerun the application, the same three numbers will be displayed. This is not a bug. It's a feature of Visual Basic that allows you to debug applications that use random numbers (if the sequence were different, you wouldn't be able to re-create the problem). To change this default behavior, call the Randomize statement at the beginning of your code. This statement will initialize the random-number generator based on the value of the computer's Timer, and the sequences of random numbers will be different every time you run the application.

If seed is negative, the Rnd() function always returns the same sequence of random numbers. As strange as this behavior may sound, you may need this feature to create repeatable random numbers to test your code. If seed is positive (or omitted), the Rnd() function returns the next random number in the sequence. Finally, if seed is zero, the Rnd() function returns the most recently generated random number.

In most cases, you don't need a random number between 0 and 1, but between two other integer values. A playing card's value is an integer in the range 1 through 13. To simulate the throw of a dice, you need a number in the range 1 through 6. To generate a random number in the range lower to upper, in which both bounds are integer numbers, use the following statement:

randomNumber = Int((upper - lower + 1) * Rnd() + lower)

The following statement displays a random number in the range 1 to 49:

0 0


  • Anna Fink
    What is a seed vb.net?
    7 years ago

Post a comment