Random Number Generator in C# with Examples

  By : Suresh Dasari
  Posted On : 05-Jun-2023

Here we will learn how to generate random numbers in c#, how to use random class in c#, how to generate random numbers within a range 0 and 1 in c#, how to generate a random number between 1 and 100 in c#, and how to generate a random alphanumeric string in c# with examples.

Random Class in C#

To generate random numbers in c#, we have a class called Random by using which we can generate random numbers. The Random class will use a pseudo random number generation algorithm to generate random data type values such as integers, floating point numbers, strings, etc.

 

The following is an example of generating random numbers using the Random class in c#.

 

using System;

namespace Tutlane
{
   class Program
   {
      static void Main(string[] args)
      {
         Random random = new Random();
         int number = random.Next();
         Console.WriteLine(number);
         Console.ReadLine();
      }
   }
}

In this example, we created a Random class instance and used the Next() method of the Random class to generate the random number in c#. The Random class Next() method will generate a random integer number every time you execute the program.

 

The Next() method will also accept the parameters to generate the random numbers within the specified range. Along with the Next() method, the Random class has different methods to generate random numbers.

 

The following table lists the different methods of Random class in c# to generate a sequence of random numbers or strings within the range based on the requirements.

 

MethodDescription
Next() It will return the non-negative random integer values within the range of 0 to Int32.MaxValue
Next(Int32) It will return the non-negative random integer that is less than the specified maximum value.
Next(Int32, Int32) It will generate the random integer values within the specified minimum and maximum range.
NextDouble() This is useful to generate the random floating-point number within the range of 0.0 to 1.0
NextByte() This will fill the elements of a specified array with a random number of bytes.

Generate Multiple Random Numbers in C#

To generate multiple random numbers, you need to call the Next() method of the Random class multiple times.

 

The following is an example of generating multiple random numbers in c#.

 

using System;

namespace Tutlane
{
   class Program
   {
      static void Main(string[] args)
      {
         Random random = new Random();
         for (int i = 0; i < 5; i++)
         {
            int number = random.Next();
            Console.WriteLine(number);
         }
         Console.ReadLine();
      }
   }
}

When you execute the above program, it will generate some random number 5 times as below.

 

1476106743
251446015
102697083
1496784150
1776922296

C# Generate Random Numbers Less than Value

By using the Next(int32) method, you can always generate random numbers less than the specified maximum value.

 

The following is an example of generating random positive numbers that are less than 500.

 

using System;

namespace Tutlane
{
   class Program
   {
      static void Main(string[] args)
      {
         Random random = new Random();
         for (int i = 0; i < 5; i++)
         {
            int number = random.Next(500);
            Console.WriteLine(number);
         }
         Console.ReadLine();
      }
   }
}

When you execute this example, you will get some random numbers that are less than 500.

 

41
57
261
498
462

C# Generate Random Numbers within the Range

By using the Next(minVal, maxVal) method, you can generate random numbers within the specified minimum and maximum range of values.

 

Following is an example of generating random numbers within the specified range in c#.

 

using System;

namespace Tutlane
{
   class Program
   {
      static void Main(string[] args)
      {
         Random random = new Random();
         for (int i = 0; i < 5; i++)
         {
            int number = random.Next(500, 1000);
            Console.WriteLine(number);
         }
         Console.ReadLine();
      }
   }
}

In this example, we are trying to get the random number between 500 to 1000 for that we defined a range by mentioning the minimum value as 500, and the maximum value as 1000.

 

When you execute this example, you will get the random results below.

 

725
915
635
926
784

If you want to generate random numbers between 1 and 100, you need to define the minimum value as 1 and the maximum value as 100 in the Next() method.

Generate Random Floating-Point Number in C#

To generate the random floating-point numbers, you need to use NextDouble() method. This method will generate random numbers between 0 and 1 as floating-point numbers.

 

The following is an example of generating the random numbers between 0 and 1 in c#.

 

using System;

namespace Tutlane
{
   class Program
   {
      static void Main(string[] args)
      {
         Random random = new Random();
         for (int i = 0; i < 5; i++)
         {
            double number = random.NextDouble();
            Console.WriteLine(number);
         }
         Console.ReadLine();
      }
   }
}

When you execute this example, you will get the random results below.

 

0.2172885776525748
0.5043349577142732
0.8165601196983577
0.4528644853611423
0.4917773797182198

Generate Random Number of Bytes in C#

By using NextBytes(), we can generate the random number of byte values in c#.

 

The following is an example of generating the random number of bytes in c#.

 

using System;

namespace Tutlane
{
   class Program
   {
      static void Main(string[] args)
      {
         Random random = new Random();
         byte[] bytes = new byte[4];
         random.NextBytes(bytes);
         foreach (var v in bytes)
         {
            Console.WriteLine(v);
         }
         Console.ReadLine();
      }
   }
}

In this example, we defined a byte array and used that to fill the number of byte values.

 

When you execute this example, you will get some random byte values as below.

 

200
62
135
118

C# Generate Random String of Letters

You can also generate a random string of letters using the Next method in c#. The following is an example of generating random characters from a string.

 

using System;

namespace Tutlane
{
   class Program
   {
      static void Main(string[] args)
      {
         Random random = new Random();
         string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
         for (int i = 0; i < 5; i++)
         {
            char randomChar = letters[random.Next(letters.Length)];
            Console.WriteLine(randomChar);
         }
         Console.ReadLine();
      }
   }
}

When you execute this example, you will get the following random characters from a string.

 

N
K
O
X
Y

C# Generate Random String with Specified Length

You can also generate a random string based on the specified length like generating a random password with 6 characters, etc. based on our requirements.

 

using System;

namespace Tutlane
{
   class Program
   {
      static void Main(string[] args)
      {
         Random random = new Random();
         string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
         string str = string.Empty;
         for (int i = 0; i < 5; i++)
         {
            char randomChar = letters[random.Next(letters.Length)];
            str += randomChar;
         }
         Console.WriteLine(str);
         Console.ReadLine();
      }
   }
}

In this example, we are generating the 5 characters string. When you execute this example, you will get the random 5 characters string as below.

 

OTHPD

C# Generate Random Alphanumeric String

While working with the user management system, we will get a requirement like generating a random alphanumeric string, random password, etc for that we need to write the code as following to generate the random passwords.

 

using System;

namespace Tutlane
{
   class Program
   {
      static void Main(string[] args)
      {
         Random random = new Random();
         string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
         string str = string.Empty;
         for (int i = 0; i < 5; i++)
         {
            char randomChar = letters[random.Next(letters.Length)];
            str += randomChar;
            int number = random.Next(10);
            str += number;
         }
         Console.WriteLine(str);
         Console.ReadLine();
      }
   }
}

In this example, we are generating random passwords by mixing the letters and numbers. When you execute this example, you will get the following random passwords.

 

K7S2J8T7T4

This is how we can use the Random class Next method to generate random strings and numbers based on our requirements.