Generating a C# random number between 1 and 100 is one of the most common beginner tasks in .NET development. Whether you are building a simple console game, creating a quiz question picker, simulating a dice roll, or testing input validation, random numbers are everywhere in programming. The good news is that C# makes this task very straightforward, especially when you use the built-in Random class from the System namespace. That said, small mistakes can cause confusing results, such as getting 0, missing 100, or receiving the same number repeatedly. This guide explains how to generate a random number between 1 and 100 correctly, why the boundaries matter, how to generate multiple numbers, and how to avoid common pitfalls That alone is useful..
Short version: it depends. Long version — keep reading.
Introduction to Random Numbers in C#
Random numbers are values produced by an algorithm that attempts to simulate unpredictability. Worth adding: in reality, most programming languages, including C#, use pseudo-random number generation, meaning the sequence is determined by an initial value called a seed. That said, if you use the same seed, you will get the same sequence of numbers. For most everyday applications, this is completely acceptable because the numbers appear random enough for games, simulations, and test data Which is the point..
When someone asks how to get a C# random number between 1 and 100, the expected range is usually clear: the result should be an integer from 1 up to and including 100. That means 1 is possible, 100 is possible, and values like 0, -5, or 101 should not appear. To achieve this, you need to understand how C# handles range boundaries.
Why the Range 1 to 100 Needs Careful Handling
The biggest source of errors when generating a random number in C# is misunderstanding how the upper limit works. So in many beginner examples, developers write Random. On the flip side, next(1, 100) and assume the result will be between 1 and 100 inclusive. In reality, the second argument is exclusive, meaning 100 will never be returned That's the whole idea..
For example:
Random random = new Random();
int number = random.Next(1, 100);
This code can produce values such as 1, 42, or 99, but it will never produce 100. If your requirement is truly “between 1 and 100 inclusive,” you need to change the upper bound to 101.
Random random = new Random();
int number = random.Next(1, 101);
This is the correct approach for a C# random number between 1 and 100 where both 1 and 100 are valid outcomes. The lower bound is inclusive, but the upper bound is exclusive, so 101 is needed to allow 100 Most people skip this — try not to..
Generating a Single Random Number in C#
The simplest way to generate one random number in C# is to use the Random class and its Next method. The Next method has several overloads, but the one that accepts two integers is ideal for this task.
using System;
class Program
{
static void Main()
{
Random random = new Random();
int randomNumber = random.Next(1, 101);
Console.WriteLine($"Random number between 1 and 100: {randomNumber}");
}
}
In this example:
Random random = new Random();creates a new random number generator.random.Next(1, 101)generates an integer from 1 to 100 inclusive.Console.WriteLinedisplays the result.
If you run this program several times, you should see different values. That said, if you create the Random object too quickly in a loop, you may accidentally get repeated values because two instances created close together may receive the same seed. This issue is discussed in more detail below Easy to understand, harder to ignore. Practical, not theoretical..
Generating Multiple Random Numbers Between 1 and 100
Often, you need more than one number. As an example, you might want to create a list of random values, simulate multiple dice rolls, or fill an array with test data. In that case, you can call Next repeatedly using the same Random instance Practical, not theoretical..
using System;
class Program
{
static void Main()
{
Random random = new Random();
for (int i = 0; i < 10; i++)
{
int randomNumber = random.Next(1, 101);
Console.WriteLine(randomNumber);
}
}
}
This code generates ten random numbers between 1 and 100. Practically speaking, the key detail is that the Random object is created once, outside the loop. The loop then reuses the same generator, which helps produce a more varied sequence It's one of those things that adds up..
If you need to store the numbers instead of printing them immediately, you can use a list:
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
Random random = new Random();
List numbers = new List();
for (int i = 0; i < 10; i++)
{
numbers.Add(random.Next(1, 101));
}
foreach (int number in numbers)
{
Console.WriteLine(number);
}
}
}
This approach is useful when you need to process the values later, such as sorting them, counting them, or displaying them in a different order Small thing, real impact..
Understanding the Random.Next Method
The Next method is the heart of generating a random integer in C#. When you call Next(minValue, maxValue), you are asking for a random integer that is