
In this tutorial, you want to acquire random numbers programmatically using the VB.NET programming language, yielding an unpredictable stream of numbers. Random numbers are useful for implementing algorithms that rely on randomness such as shuffling.
First, this program demonstrates the usage of Random type as a field in a module. For modules or classes, it is best to store a Random number generator as a field. This is because the Random instance will not repeat itself as much if called from multiple member functions. The program writes three random numbers to the screen.
This VB example uses the Random type. Random is used as a field.
Program: random numbers [VB.NET]
Module Module1
Sub Main()
' Write three random numbers.
F()
F()
F()
End Sub
''' <summary>
''' Write the next random number generated.
''' </summary>
Private Sub F()
' Call Next method on the random object instance.
Console.WriteLine(_r.Next)
End Sub
''' <summary>
''' Store a random number generator.
''' </summary>
Private _r As Random = New Random
End Module
Output
1284029964
984949494
1322530626
More options on Random. Here we discuss the Next method on the Random type in the VB.NET language. You can use overloaded versions of the Next method to constrain the range of the Random number yielded. The first argument is the inclusive lower bound; the second argument is the exclusive upper bound. Therefore, if you pass 1, 3, you will get values 1 or 2.

In this tutorial, we looked at the Random type in the VB.NET language and saw how you can use this to generate a stream of random numbers. We saw that it is important to use the Random type as a field in some situations, to reduce the possibility of repetition. Also, the fewer Random number generators you allocate, the less memory will be used for them.
VB.NET Tutorials