Home

Built-In Classes: Random

     

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:

using System;

class Program
{
    static int Main()
    {
        Random rndNumber = new Random();
       
        return 0;
    }
}

After creating 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 virtual int Next();

This method generates a randomly selected integer between 0 and the MinValue value of the int data type. Here is an example:

using System;

class Program
{
    static int Main()
    {
        Random rndNumbers = new Random();
        int    rndNumber = rndNumbers.Next();

        Console.WriteLine("Number: {0}", rndNumber);
       
        return 0;
    }
}

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:

using System;

class Program
{
    static int Main()
    {
        Random rndNumbers = new Random();
        int rndNumber = 0;

        for (int nbr = 1; nbr < 9; nbr++)
        {
            rndNumber = rndNumbers.Next();
            Console.WriteLine("Number: {0}", rndNumber);
        }

        return 0;
    }
}

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:

using System;

class Program
{
    static int Main()
    {
        Random rndNumbers = new Random();
        int rndNumber = rndNumbers.Next();

        Console.WriteLine("Number: {0}", rndNumber);

        return 0;
    }
}

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 Random(int Seed);

Based on this, to specify a seed, when declaring a Random variable, pass a constant integer to the constructor. Here is an example:

using System;

class Program
{
    static int Main()
    {
        Random rndNumbers = new Random(20);
        int rndNumber = rndNumbers.Next();

        Console.WriteLine("Number: {0}", rndNumber);

        return 0;
    }
}

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:

using System;

class Program
{
    static int Main()
    {
        Random rndNumbers = new Random(20);
        int rndNumber = 0;


        for (int nbr = 1; nbr < 5; nbr++)
        {
            rndNumber = rndNumbers.Next();
            Console.WriteLine("Number: {0}", rndNumber);
        }

        return 0;
    }
}

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 virtual int Next(int maxValue);

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:

using System;

class Program
{
    static int Main()
    {
        Random rndNumbers = new Random();
        int rndNumber = 0;


        for (int nbr = 1; nbr < 9; nbr++)
        {
            rndNumber = rndNumbers.Next(20);
            Console.WriteLine("Number: {0}", rndNumber);
        }

        return 0;
    }
}

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 virtual int Next(int minValue, int maxValue);

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:

using System;

class Program
{
    static int Main()
    {
        Random rndNumbers = new Random();
        int rndNumber = 0;


        for (int nbr = 1; nbr < 9; nbr++)
        {
            rndNumber = rndNumbers.Next(6, 18);
            Console.WriteLine("Number: {0}", rndNumber);
        }

        return 0;
    }
}

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 . . .

Notice that the numbers are between 6 and 18.

 
 

Home Copyright © 2009-2011 FunctionX