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