Seeding a Random Number

Random is a class within the System namespace. It is also an object which must be created with the New keyword.
Random() without an argument is based on the system timer, which is constantly changing. It is left blank for games of chance. To repeat the same sequence of random numbers, place a seed number, which can be any integer, inside the ().

Dim randomvariable As New Random(seednumber)
 
Thanks for your helps! But how can I randomize an integer number from 1 to 9? I did it previously with Rnd() function. But after changing the datatype of the variables from integer to New Random () I can't use the Rnd() function anymore? How can I do these?
 
Last edited:
The range of the random number is established with the Next() method, which takes two arguments - the starting number, and the ending number + 1. In order to include the last number in the range, you must add 1 to the ending number. The value of the first argument must be less than the value of the second. The randomly selected number must also be declared. Example:

Dim randvar As New Random()
Dim num As Integer
num = randvar.Next(first, last + 1)
Console.WriteLine(num)

The number one before last is included in the range. To select a number between 1-10, the last argument must be 11.
For example, to select a number between 1 and 9 you would do:
num = randvar.Next(1, 10)
 
Back
Top