The following is a listing of a random number program that creates an object and assigns it to the variable rnGenerator. The object is an instance of the class Random, and it is created at run time, not design time. (Instances of the class Random are objects that can generate random numbers).
Dim rnGenerator As Random = New Random() Private Sub btn01_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn01.Click lbl01.Text = CStr(rnGenerator.NextDouble) End Sub Private Sub btn510_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn510.Click lbl510.Text = CStr(rnGenerator.Next(5, 11)) End Sub
The first line declares an object variable called rnGenerator and constructs a New instance of the class Random. It then assigns that instance to the object variable called rnGenerator.
The two event handlers use the newly created object, rnGenerator, to generate random numbers between 0 and 1 and between 5 and 10 respectively.
The first event handler uses the Nextdouble method of rnGenerator. Nextdouble returns a double-precision number that is greater than 0 and less than 1. It is uniformly distributed within that range.
The second event handler uses the Next method of rnGenerator. Next has two arguments, and returns a random integer between them.
Note that the number returned is less than the second argument, so we have to use 5 and 11 to generate numbers between 5 and 10.
(To me it would seem more natural to have the second argument be "less than or equal" so you could use 5, 10 to generate numbers between 5 and 10; however, the Microsoft programmer who programmed the Next method felt otherwise).