Home > Article > Backend Development > C# random
The class used to generate random integers using pre-defined methods is called a random class in C#. Where Next() method is the most commonly used method in the Random class to generate the random integers which can be overloaded in three forms such as Next() method that can return an integer value randomly in between the range −2,147,483,648 and +2,147,483,648, Next(int max) method is one of the other two forms that can return an integer value less than the value specified as the max, Next(int min, int max) is another form that returns an integer value which is in between the range of values specified as min and max.
Syntax
The syntax is as follows:
Random.Next(); Random.Next(int max); Random.Next(int min, int max);
Explanation: where max is the value below which the random integer value that must be returned by the Next() method lies, min and max specifies the range between which the random integer value that must be returned must lie.
Whenever there is a need to generate random integers by making use of predefined methods, we make use of Random class in C#. The next () method is the most commonly used method in the Random class to generate random integers which can be overloaded in three forms. Next() method that can return an integer value randomly in between the range −2,147,483,648 and +2,147,483,648. Next(int max) method is one of the other two forms that can return an integer value less than the value specified as max. Next(int min, int max) is another form that returns an integer value which is in between the range of values specified as min and max.
Below are the examples mentioned :
C# program to demonstrate the Random class by making use of Next() method to generate random integer between −2,147,483,648 and +2,147,483,648:
Code:
using System; //a class called check is defined public class check { //main method is called within which an instance of the random class is created to be able to make use of Next() method public static void Main() { Random ran = new Random(); //Next() method is called to generate a random integer value between −2,147,483,648 and +2,147,483,648 and stored in an integer variable int num= ran.Next(); //The random number generated by making use of Next() method of random class is displayed as the output Console.WriteLine("The Random Number generated by making use of Next() method of random class is: "+num); } }
Output:
Explanation: In the above program, a class called check is defined. Then the main method is called within which an instance of the Random class is created to be able to make use of the Next() method. Then the Next() method is called to generate a random integer value between −2,147,483,648 and +2,147,483,648 and stored in an integer variable. Then the random number generated by making use of the Next() method of a random class is displayed as the output. The output is shown in the snapshot above.
C# program to demonstrate the Random class by making use of Next(int max) method to generate random integer within the specified value max:
Code:
using System; //a class called check is defined public class check { //main method is called within which an instance of the Random class is created to be able to make use of Next(int max) method public static void Main() { Random ran = new Random(); // Next(int max) method is called to generate a random integer value which is within the specified value max and stored in an integer variable int num= ran.Next(50); //The random number generated by making use of Next(int max) method of random class is displayed as the output Console.WriteLine("The Random Number within the specified range 50 generated by making use of Next(int max) method of random class is: "+num); } }
Output:
Explanation: In the above program, a class called check is defined. Then the main method is called within which an instance of the Random class is created to be able to make use of the Next(int max) method. Then the Next(int max) method is called to generate a random integer value whose value is within the value specified as max and stored in an integer variable. Then the random number generated by making use of the Next(int max) method of a random class is displayed as the output. The output is shown in the snapshot above.
C# program to demonstrate the Random class by making use of Next(int min, int max) method to generate a random integer within the range specified by the values min and max:
Code:
using System; //a class called check is defined public class check { //main method is called within which an instance of the Random class is created to be able to make use of Next(int min, int max) method public static void Main() { Random ran = new Random(); // Next(int min, int Maxx) method is called to generate a random integer value which is within the specified range of values min and max and stored in an integer variable int num= ran.Next(50,100); //The random number generated by making use of Next(int min,int max) method of random class is displayed as the output Console.WriteLine("The Random Number within the specified range between min and max generated by making use of Next(int min, int max) method of a random class is: "+num); } }
Output:
Explanation: In the above program, a class called check is defined. Then the main method is called within which an instance of the Random class is created to be able to make use of Next(int min, int max) method. Then the Next(int min, int max) method is called to generate a random integer value whose value is within the range specified as min and max and stored in an integer variable. Then the random number generated by making use of Next(int min, int max) method of a random class is displayed as the output. The output is shown in the snapshot above.
In this tutorial, we understand the concept of Random in C# through definition, syntax, and working of Random and its methods through programming examples and their outputs.
The above is the detailed content of C# random. For more information, please follow other related articles on the PHP Chinese website!