Different programming languages allow for the generation of random integers. There are classes and built-in methods for this purpose. To get random numbers, we may combine these methods with objects of these types. The “Random” class in C# enables us to produce random numbers as well. The “Random” class has a variety of methods for producing random integers. We can also create floating-point numbers and negative numbers using several techniques.
Here, we’ll discuss the generation of random numbers in C#.
How to Generate Random Numbers in C#
In C#, there is a class called “Random” which helps us to generate random numbers in C#. We can use this class with different methods to generate random numbers according to our needs. A few methods are:
These three methods help us to produce random integers. We can also generate the float values by using:
Now we will discuss the working of each method one by one.
Method 1: Next() Method with Random Class
The “Next()” method is used to produce positive integers in C#. To call this method we need to call the constructor of a “Random” class and an object. Then, we call the “Next()” method with that object and generate random positive integers. Here is a C# code example to understand this concept:
public class Generate_random_numbers
{
public static void Main(string[] args)
{
Random r = new Random();
Console.WriteLine(r.Next());
}
}
In the above-generated code:
-
- Firstly, we have called the “System” library at the start of the program.
- Then, we created a “Generate_random_numbers” class.
- In this class, a “Main()” method is called.
- Afterward, we call the constructor or “Random” class and create an object “r”.
- Lastly, we used this object “r” with the “Next()” method to generate random positive integers.
Here is the output to show the result of the above code:
To generate random numbers in multiple lines, we use loops:
public class Generate_random_numbers
{
public static void Main(string[] args)
{
Random r = new Random();
for (int i=1;i<=10;i++){
Console.WriteLine("Value "+i+": "+r.Next());
}
}
}
In this code:
-
- We used the above example’s code and added a “for loop” in it.
- In this loop, we declare a variable “i” to set the loop from 1 to 10 to generate 10 random numbers.
- Then, we called the “Next()” method to generate integers.
Here is the output of this code:
Method 2: Next(int) Method with Random Class
This method also works the same as the “Next()” method. The only difference is in this method we need to specify a “value” to generate a random integer that is less than that value. Now, have a look at the code to understand the working of this method:
public class Generate_random_numbers
{
public static void Main(string[] args)
{
Random r = new Random();
Console.WriteLine(r.Next(10));
}
}
In this example:
-
- We used the “System” library and created a class “Generate_random_numbers” which has a “Main()” method.
- Inside the “Main()” method the “Random” class constructor is called and an object “r” is created.
- We use the method “Next(int)” with that object and specify the value with “10” which means that the randomly created integer should be less than “10”.
Let’s see the result to check whether the value is less than 10 or not:
The output is “5” which is less than “10”.
We can also generate multiple integers using a loop. Try the following example to generate multiple integers:
public class Generate_random_numbers
{
public static void Main(string[] args)
{
Random r = new Random();
for (int i=1;i<=10;i++){
Console.WriteLine("Value "+i+": "+r.Next(10));
}
}
}
We have used the same example. Additionally:
-
- We added “for loop” after the constructor.
- Inside the loop, we call the method “Next(int)” and set the value to “10” to generate numbers less than “10”.
The outcome indicates that every number is less than “10” and is generated randomly.
Method 3: Next(int1, int2) Method with Random Class
The previous two methods are used to generate positive integers. In this method, we can specify the range of numbers which means we can also generate the negative numbers. Let’s see how it works:
public class Generate_random_numbers
{
public static void Main(string[] args)
{
Random r = new Random();
Console.WriteLine(r.Next(-10,10));
}
}
Again, this is the same example. The only difference is:
-
- We have set the range from “-10” to “10”. Which means the number should be in between this range.
The output is “-8” which lies between the specified range.
Now we will generate random numbers within a specified range inside the loop:
public class Generate_random_numbers
{
public static void Main(string[] args)
{
Random r = new Random();
for (int i=1;i<=10;i++){
Console.WriteLine("Value "+i+": "+r.Next(-10,10));
}
}
}
In the above example:
-
- We just use “for loop” and inside the for loop use the method “Next(-10,10)” to generate 10 random numbers within the “-10” to “10” range.
Output
The above output shows that the code is working correctly and the output is within the specified range.
Method 4: NextDouble() Method with Random Class
The “NextDouble()” method is used to generate random numbers with decimal points. The method works the same as the “Next()” method. The only difference is, it will generate the numbers with decimal points within the (0.0-1.0) range. The following code elaborates on this method:
public class Generate_random_double_numbers
{
public static void Main(string[] args)
{
Random r = new Random();
Console.WriteLine(r.NextDouble());
}
}
In the above-generated code:
-
- We have used the “System” library and declared a class “Generate_random_double_numbers” and a method “Main()”.
- Ext, we called the “Random” class constructor and create an object “r”.
- Then, we called the method “NextDouble()” along with “r”.
- The output should fall between 0.0 and 0.1.
Output
Now let’s see multiple double numbers to understand this method:
public class Generate_random_double_numbers
{
public static void Main(string[] args)
{
Random r = new Random();
for (int i=1;i<=10;i++){
Console.WriteLine("Value "+i+": "+r.NextDouble());
}
}
}
This code is the same as above. Additionally:
-
- We have a “for loop” to generate 10 numbers.
- Called the “NextDouble()” method inside it.
As we can see from the result above, all of the numbers fall inside the (0.0-0.1) range.
Conclusion
We can generate a random number or multiple random numbers in C#. For this purpose “Next()”, “Next(int)”, and “Next(int1, int2)” methods are used with the “Random” class’s object. The “NextDouble()” function may be used to produce floating point random numbers as well. All of it has to do with creating random numbers in C#.