Development

How to generate a random number in C#

Captain Salem 2 min read

How to generate a random number in C

C# pronounced as “See Sharp,” is a free and open-source objected oriented, type-safe programming language. C# has deep roots in Java, C, and C++. You will often a very similar syntax and data types of such languages. It is, therefore, a powerful language that is used to build a plethora of applications, including Web Applications, Desktop Apps, Games, etc.

In this tutorial, we will learn how to use the Random Class in C# to generate a pseudo-random integer.

C# Random Class

The Random class in C# provides us with a set of predefined methods that allows us to generate pseudo-random numbers.

The class definition is as shown:

public class Random

In the Random class, we have access to the Next() method, which allows us to generate a random integer. Let us explore how we can use this function to generate a random number.

C# Random.Next() Method

The function definition is as shown:

public virtual int Next ();

The function then returns a 32-bit signed non-negative integer greater than or equal to 0.

Example Function Usage

The following example code shows how we can use the Next method to generate a random number and print them to the console.

class RandomNumber {
    static void Main()
    {
        System.Random random = new System.Random();
        System.Console.WriteLine(random.Next());
    }
}

The query above should generate one random integer as shown:

284772901

C# Generate Random Integer Less than a Specific Max

We can also use the syntax below to generate a random integer less than the max parameter:

Next(int max)

This allows you to specify the custom maximum value generated by the function. The following snippet demonstrates how to apply the above function.

class RandomNumber {
    static void Main()
    {
        System.Random random = new System.Random();
        System.Console.WriteLine("Generating 10 rnadom values: ");
        for (int i = 1; i <= 10; i++)
        {
            Console.WriteLine("Position[{0}] -> {1}", i, random.Next(50));
        }
    }
}

Once we run the code above, it should generate random values less than 50. An example output is as shown:

Generating 10 random values:
Position[1] -> 30
Position[2] -> 13
Position[3] -> 42
Position[4] -> 33
Position[5] -> 20
Position[6] -> 10
Position[7] -> 31
Position[8] -> 23
Position[9] -> 35
Position[10] -> 8

C# Generate Random Number within a Specific Range

Sometimes, you may need to define a custom range for the random value generated. Luckily, we can use the syntax below to do exactly that.

Next(int min, int max)

In this case, the function allows you to specify the minimum and maximum values that can be generated.

For example, to generate 10 random integers in the range of 50 and 100, we can run the following code:

class RandomNumber {
    static void Main()
    {
        System.Random random = new System.Random();
        System.Console.WriteLine("Generating 10 rnadom values: ");
        for (int i = 1; i <= 10; i++)
        {
            Console.WriteLine("Position[{0}] -> {1}", i, random.Next(50, 100));
        }
    }
}

Result:

Generating 10 rnadom values: 
Position[1] -> 76
Position[2] -> 94
Position[3] -> 77
Position[4] -> 63
Position[5] -> 60
Position[6] -> 77
Position[7] -> 82
Position[8] -> 76
Position[9] -> 96
Position[10] -> 67

Conclusion

In this article, you learned how you could use the Next function from the C# Random class to generate a pseudo-random integer. You also learned how to specify the minimum and maximum values to generate a random integer in a given range.

We hope you enjoyed this tutorial. If you did, leave us a comment below and share it with your friends.

Thanks for reading!!

Share
Comments
More from Cloudenv

Cloudenv

Developer Tips, Tricks and Tutorials.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Cloudenv.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.