Searching...
Monday, May 27, 2013

How to Generate Random Number in Visual Basic NET

Generating random number in Visual Basic .NET can be done by writing x = Rnd() * 9 . It will generate any real number between 0 to 8 (I'm not sure whether 9 is included or not) and also decimal number. If you want to exclude the decimal number, you can add Int, so it will be x = Int(Rnd() * 9) . It's generating number 8, 6, 2, 3, 1, ... . But the problem is when the code is closed and restarted, generated number still 8, 6, 2, 3, 1, ...

Other procedure is by writing:

Dim RandomClass As New Random
x = RandomClass.Next(1, 100)
"It will generate random number between 1 and 100."

or

Dim RandomClass As New Random()
x = RandomClass.Next()
"It will generate random number between 1 and 2,147,483,647."

1 comments:

 
Back to top!