0
0
CsharpProgramBeginner · 2 min read

C# Program to Generate Random Number Easily

In C#, you can generate a random number by creating an instance of Random and calling Next(), like Random rnd = new Random(); int number = rnd.Next();.
📋

Examples

InputNo input needed
OutputRandom number like 123456789
InputGenerate number between 0 and 9
OutputRandom number like 7
InputGenerate number between 1 and 100
OutputRandom number like 42
🧠

How to Think About It

To generate a random number in C#, you first create a Random object which acts like a random number generator. Then you call its Next() method to get a random integer. You can also specify a range by giving minimum and maximum values to Next(min, max).
📐

Algorithm

1
Create a Random object.
2
Call the Next() method to get a random integer.
3
Optionally, specify minimum and maximum values to limit the range.
4
Print or return the generated random number.
💻

Code

csharp
using System;

class Program
{
    static void Main()
    {
        Random rnd = new Random();
        int randomNumber = rnd.Next(1, 101); // 1 to 100
        Console.WriteLine("Random number between 1 and 100: " + randomNumber);
    }
}
Output
Random number between 1 and 100: 57
🔍

Dry Run

Let's trace generating a random number between 1 and 100 through the code.

1

Create Random object

Random rnd = new Random(); // rnd is ready to generate numbers

2

Generate random number

int randomNumber = rnd.Next(1, 101); // randomNumber gets a value like 57

3

Print the number

Console.WriteLine prints: Random number between 1 and 100: 57

StepActionValue
1Create Random objectrnd ready
2Call rnd.Next(1, 101)randomNumber = 57
3Print output"Random number between 1 and 100: 57"
💡

Why This Works

Step 1: Create Random instance

The Random class provides methods to generate random numbers. Creating an instance prepares the generator.

Step 2: Generate number with Next()

Calling Next(min, max) returns a random integer from min (inclusive) to max (exclusive).

Step 3: Output the result

Printing the number shows the random value to the user or program.

🔄

Alternative Approaches

Use NextDouble() for random decimal
csharp
using System;

class Program
{
    static void Main()
    {
        Random rnd = new Random();
        double randomDouble = rnd.NextDouble(); // 0.0 to 1.0
        Console.WriteLine("Random decimal between 0 and 1: " + randomDouble);
    }
}
Generates a random decimal between 0 and 1 instead of an integer.
Use static Random.Shared (C# 9+)
csharp
using System;

class Program
{
    static void Main()
    {
        int randomNumber = Random.Shared.Next(1, 101);
        Console.WriteLine("Random number between 1 and 100: " + randomNumber);
    }
}
Uses the shared Random instance to avoid creating multiple objects, improving performance.

Complexity: O(1) time, O(1) space

Time Complexity

Generating a random number is a constant time operation because it does not depend on input size.

Space Complexity

Only a small fixed amount of memory is used for the Random object and the generated number.

Which Approach is Fastest?

Using Random.Shared is fastest for multiple calls because it avoids creating new objects.

ApproachTimeSpaceBest For
Random instance with Next()O(1)O(1)Simple random integers
Random.NextDouble()O(1)O(1)Random decimal numbers
Random.SharedO(1)O(1)Multiple random calls with better performance
💡
Create one Random object and reuse it to get better random numbers and avoid repetition.
⚠️
Creating a new Random object inside a loop can produce the same number repeatedly because of the same seed.