How to generate random numbers in C#
Overview
The C# language provides a built-in class, Random, for generating random numbers based on a seed value. By default, the Random class uses a system clock to generate a seed value.
The Random class provides the following methods to generate random numbers.
1. The Next() method
This method generates a random positive number within the range -2,147,483,648 to 2,147,483,647.
Syntax
Random random = new Random();int num = random.Next();
Example
Let's see an example of the Next() method in the code snippet below:
using System;class Example{static void Main(){// instantiate Random classRandom random = new Random();// generate a random numberint n = random.Next();// print the number of consoleConsole.WriteLine("Random number: {0}", n);}}
Explanation
Inside the main() function:
- Line 8: We instantiate the
Randomclass and create an object calledrandom. - Line 11: We generate a random positive number using the
Next()method. - Line 14: We print the random number on the console.
2. The Next(int) method
This method generates a random number that is less than the specified integer. Here, the specified integer acts as the upper bound.
Syntax
Random random = new Random();int num = random.Next(int);
Example
Let's see an example of the Next(int) method in the code snippet below:
using System;class Example{static void Main(){// instantiate Random classRandom random = new Random();// generate a random numberint n = random.Next(50);// print the number of consoleConsole.WriteLine("Random number: {0}", n);}}
Explanation
Inside the main() function:
- Line 8: We instantiate the
Randomclass and create an object calledrandom. - Line 11: We generate a random number using the
Next(int)method. - Line 14: We print the random number on the console.
3. The Next(int, int) method
This method generates a random number within the specified range. Here, the first integer acts as the lower bound and the second integer acts as the upper bound.
Syntax
Random random = new Random();int num = random.Next(int, int);
Example
Let's see an example of the Next(int, int) method in the code snippet below:
using System;class Example{static void Main(){// instantiate Random classRandom random = new Random();// generate a random numberint n = random.Next(20, 30);// print the number of consoleConsole.WriteLine("Random number: {0}", n);}}
Explanation
Inside the main() function:
- Line 8: We instantiate the
Randomclass and create an object calledrandom. - Line 11: We generate a random number using the
Next(int, int)method in the range (20, 30). - Line 14: We print the random number on the console.
4. The NextDouble() method
This method generates a random floating number within the range (0.0, 1.0).
Syntax
Random random = new Random();int num = random.NextDouble();
Example
Let's see an example of the NextDouble() method in the code snippet below:
using System;class Example{static void Main(){// instantiate Random classRandom random = new Random();// generate a random numberdouble n = random.NextDouble();// print the number of consoleConsole.WriteLine("Random number: {0}", n);}}
Explanation
Inside the main() function:
- Line 8: We instantiate the
Randomclass and create an object calledrandom. - Line 11: We generate a random floating number using the
NextDouble()method. - Line 14: We print the random number on the console.