|
Imagine you have a series of numbers, such these: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, and 20. Imagine you want to select one of these numbers, any of them. A number is referred to as random if it has been selected from a pool without a specific pattern to follow. For example, if you decide to select the value 17 from this list, if there was an exact reason that number was selected, then it is not considered random. In reality, it is difficult for a number to qualify as random. For this reason, most random numbers are referred to as pseudo-random.
|
To support the ability to create or choose a random number, the .NET Framework provides the
Random class. To start, you can declare a variable of this class, using one of its two constructors. Here is an example that uses the default constructor:
Module Exercise
Public Function Main() As Integer
Dim rndNumbers As Random = New Random
Return 0
End Function
End Module
After declaring the variable, you can start getting numbers from it. To do this, you call the
Next() method, which is overloaded in three versions. One of the versions of this method takes no argument and its syntax is:
Public Overridable Function Next As Integer
This method generates a randomly selected integer between 0 and the
MinValue value of the Integer data type. Here is an example:
REM Imports System
Module Exercise
Public Function Main() As Integer
Dim rndNumbers As Random = New Random
Dim rndNumber As Integer = rndNumbers.Next()
Console.Write("Number: ")
Console.WriteLine(rndNumber.ToString())
Return 0
End Function
End Module
Here is an example of running the program:
Number: 1369872590
Press any key to continue . . .
In the same way, you can call this version of the Next() method repeatedly to get random. Here is an example:
Module Exercise
Public Function Main() As Integer
Dim nbr As Integer
Dim rndNumber As Integer = 0
Dim rndNumbers As Random = New Random
For nbr = 1 To 9
rndNumber = rndNumbers.Next()
Console.Write("Number: ")
Console.WriteLine(rndNumber.ToString())
Next
Return 0
End Function
End Module
Here is an example of running the program:
Number: 1924504148
Number: 1257846191
Number: 424740120
Number: 1009211682
Number: 544356245
Number: 708951978
Number: 759684741
Number: 1325535324
Press any key to continue . . .
The Seed of a Random Number
|
|
Consider the following program:
Module Exercise
Public Function Main() As Integer
Dim rndNumbers As Random = New Random
Dim rndNumber As Integer = rndNumbers.Next()
Console.Write("Number: ")
Console.WriteLine(rndNumber.ToString())
Return 0
End Function
End Module
Here is an example of running the program:
Number: 573991745
Press any key to continue . . .
Here is another example of running the same program:
Number: 334223329
Press any key to continue . . .
Notice that the numbers generated are different. When creating a program that repeatedly gets a series of random numbers, you may (or may not) want the Random class to generate the same number over and over again. A seed is a constant value that controls whether a random generation would produce the same result every time it occurs. For example, using a seed, you can impose it upon the Random class to generate the same number every time the Next() method is called.
To support the ability to use a seed, the Random class is equipped with a second constructor whose syntax is:
Public Sub New(Seed As Integer)
Based on this, to specify a seed, when declaring a Random variable, pass a constant integer to the constructor. Here is an example:
Module Exercise
Public Function Main() As Integer
Dim rndNumbers As Random = New Random(20)
Dim rndNumber As Integer = rndNumbers.Next()
Console.WriteLine("Number: {0}", rndNumber)
Return 0
End Function
End Module
Here is one example of running the program:
Number: 375271809
Press any key to continue . . .
Here is another example of running the same program:
Number: 375271809
Press any key to continue . . .
Notice that the numbers are the same. Consider this program also:
REM Imports System
Module Exercise
Public Function Main() As Integer
Dim nbr As Integer
Dim rndNumber As Integer = 0
Dim rndNumbers As Random = New Random(20)
For nbr = 1 To 5
rndNumber = rndNumbers.Next()
Console.Write("Number: ")
Console.WriteLine(rndNumber.ToString())
Next
Return 0
End Function
End Module
Here is one example of running the program:
Number: 375271809
Number: 1472524622
Number: 1605850688
Number: 1776011503
Press any key to continue . . .
Here is another example of running the same program:
Number: 375271809
Number: 1472524622
Number: 1605850688
Number: 1776011503
Press any key to continue . . .
Notice that the sequences are the same. In both cases, this indicates that, if you specify a seed, the Random class would generate the same number or the same sequence of numbers.