How to generate random numbers in c#.net within a range without repeating?

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.

  1. Open Visual Studio.
  2. Click on “File” -> “New” -> “Project…”
  3. 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 from start to end. Then, it uses the System.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.
generate random numbers in c#.net within a range without repeating
generate random numbers in c#.net within a range without repeating

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: