This C#.Net tutorial explains, how to generate random numbers in c#.net within a range without repeating.
Generate random numbers in c#.net within a range without repeating
Here is the step-by-step tutorial on how to generate random numbers in c#.net within a range without repeating.
Step 1: Start Visual Studio and create a new Console Application.
- Open Visual Studio.
- Click on “File” -> “New” -> “Project…”
- Select “Console Application” and name it “RandomNumberGenerator”. Click “OK”.
Step 2: Open the newly created project and navigate to your Program.cs file.
Step 3: Replace the contents of the Program.cs file with the following code:
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
// Define the range
int start = 1;
int end = 100;
// Call the function to generate random numbers
var randomNumbers = GenerateRandomNumbers(start, end);
// Print the generated random numbers
foreach (var number in randomNumbers)
{
Console.WriteLine(number);
}
}
static IList<int> GenerateRandomNumbers(int start, int end)
{
// List to hold the numbers
List<int> numbers = new List<int>();
// Fill the list with sequential numbers
for (int i = start; i <= end; i++)
{
numbers.Add(i);
}
// The list to hold the random numbers
List<int> randomNumbers = new List<int>();
// Instantiate random number generator
Random rnd = new Random();
// Loop until there are no more numbers to choose
while (numbers.Count > 0)
{
// Choose a random index
int index = rnd.Next(numbers.Count);
// Take the number at the random index
randomNumbers.Add(numbers[index]);
// Remove the number from the original list
numbers.RemoveAt(index);
}
return randomNumbers;
}
}
This code will generate and print 100 unique random numbers in the range 1 to 100.
Here is how it works:
GenerateRandomNumbers(int start, int end)
function generates the random numbers. It begins by initializing a list with the numbers fromstart
toend
. Then, it uses theSystem.Random
class to select a random index from the list, remove the number at that index (ensuring uniqueness), and add it to the random numbers list.Main(string[] args)
function calls the random number generator function and prints the generated random numbers to the console.
Step 4: Run your program.
- Click “Debug” on the menu, then select “Start Debugging” or simply press F5 to start your program.

This should generate and print unique random numbers from 1 to 100. If you want to change the range or the quantity of the numbers, simply modify the start
and end
values in the Main method.
Conclusion
By following this tutorial, you have learned how to generate a set of unique random numbers within a specified range in C#.Net.
You may like the following C#.net tutorials:
- Convert String to DateTime in C#.NET
- remove first and last character from string using C#.net
- Get the First and Last Letter of a String in C#.NET
Bijay Kumar is a renowned software engineer, accomplished author, and distinguished Microsoft Most Valuable Professional (MVP) specializing in SharePoint. With a rich professional background spanning over 15 years, Bijay has established himself as an authority in the field of information technology. He possesses unparalleled expertise in multiple programming languages and technologies such as ASP.NET, ASP.NET MVC, C#.NET, and SharePoint, which has enabled him to develop innovative and cutting-edge solutions for clients across the globe. Read more…