In the previous article, I mentioned how to Remove Duplicates from Array in C# but in this article, I have mentioned how we can generate random number in C# using the Random class, it allows us to Random numbers or it can also generate other data types including strings. 

Using Random Class (pseudo-random number generator), we can generate random numbers in between range or from starting or without providing any range.

Random class constructors have two overloaded forms. It takes either no value or it takes a seed value. This class has the following methods:

  • Random.Next(): Returns a non-negative random integer.
  • Random.NextBytes(): Returns an array of bytes filled with random numbers.
  • Random.NextDouble(): Returns a random floating-point number that is greater than or equal to 0.0, and less than 1.0.
  • Random.NextInt64(): Returns a non-negative random integer, but a 64-bit signed integer.
  • Random.NextSingle(): Returns a single-precision floating point number that is greater than or equal to 0.0, and less than 1.0.

Now, let's look at a few random number generation examples using the above methods.

Generate Random Number in C#

Let's start with a basic example, in this example, we will use Random.Next(), so we can simply get a random number

using System;

namespace RandomNumber
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Random rnd = new Random(); //create random class object
            int num = rnd.Next(); // call .Next() to get int
            Console.WriteLine(num); //print
        }
    }
}

Output:

973497532

Generate Random Number in between the range

Now, suppose if you want to generate a few random numbers using C# in between a range from 10 to 100 only, then you can use below C# code

using System;

namespace RandomNumber
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Random r = new Random();
            int num = r.Next(10, 100);
            Console.WriteLine(num);
        }
    }
}

Output can be anything in between 10 to 100, here is what I see in my local machine

57

If you want to generate a number less than 10, for example, then simply do as below

using System;

namespace RandomNumber
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Random r = new Random();
            int num = r.Next(10); // returns number less than 10
            Console.WriteLine(num);
        }
    }
}

output:

6

Generate a Random Double

If we want to generate random double value, then we can use .NextDouble()

using System;

namespace RandomNumber
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Random r = new Random();
            double num = r.NextDouble(); // returns number less than 1
            Console.WriteLine(num);
        }
    }
}

Output:

0.10010553016332235

generate-random-number-csharp

That's it.

You may also like to read:

Various star pattern program in c#

How to read pdf file in C#? (Working example using iTextSharp)

Read excel file in C# (.XLSX or .XLS using OLEDB / EPPlus or Interop)

C# Regex Examples and Basics

How to insert C# variable value in between string properly?

Bootstrap Pop Up Modal Validation in ASP.NET Core MVC