Home

Built-In Classes: The Random Class

 

Introduction

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.

 

Getting a Random Number

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.

 
 
 

 

Generating Random Numbers in a Range of Numbers

So far, we have been using any number that would fit an integer. In some assignments, you may want to restrict the range of numbers that can be extracted. Fortunately, the Random class allows this. Using the Random class, you can generate random positive numbers up to a maximum of your choice. To support this, the Random class is equipped with another version of the Next() method whose syntax is:

Public Overridable Function Next(maxValue As Integer) As Integer

The argument to pass to the method determines the highest integer that can be generated by the Next() method. The method returns an integer. Here is an example that generates random numbers from 0 to 20:

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

        For nbr = 1 To 9
            rndNumber = rndNumbers.Next(20)
            Console.Write("Number: ")
            Console.WriteLine(rndNumber.ToString())
        Next

        Return 0
    End Function

End Module

Here is an example of running the program:

Number: 1
Number: 7
Number: 1
Number: 16
Number: 14
Number: 19
Number: 3
Number: 1
Press any key to continue . . .

The above version of the Next() method generates numbers starting at 0. If you want, you can specify the minimum and the maximum range of numbers that the Next() method must work with. To support this, the Random class is equipped with one more version of this method and that takes two arguments. Its syntax is:

Public Overridable Function Next(minValue As Integer, _
				 maxValue As Integer) As Integer

The first argument specifies the lowest value that can come from the range. The second argument holds the highest value that the Next() method can generate. Therefore, the method would operate between both values. Here is an example that generates random numbers from 6 to 18:

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(6, 18)
            Console.Write("Number: ")
            Console.WriteLine(rndNumber.ToString())
        Next

        Return 0
    End Function

End Module

Here is an example of running the program:

Number: 17
Number: 9
Number: 8
Number: 15
Number: 10
Number: 9
Number: 13
Number: 11
Press any key to continue . . .
 
 
   
 

Home Copyright © 2009-2016, FunctionX, Inc.