I want to generate unique random numbers in C#. Once a number is generated, then it must not generate again.
Loading
I want to generate unique random numbers in C#. Once a number is generated, then it must not generate again.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Sachin SinghRank #66 · 26.2K reputationPosted Jan 16, 2023, 12:35 AM EST
check this
public class RandomNumberGenerator { public Random a = new Random(); public List randomList = new List(); int x= 0; public int GetRandomInt(int min,int max) { x= a.Next(min, max); if (!randomList.Contains(x)) randomList.Add(x); return randomList[x]; } }Amit MohantyRank #112 · 20K reputationPosted Jan 16, 2023, 12:36 AM EST
You can use the
System.Randomclass to generate random numbers in C#, and then store the generated numbers in a data structure such as aHashSetto ensure that each number is unique.int totalNumbers = 10; HashSet uniqueNumbers = new HashSet(); Random random = new Random(); while (uniqueNumbers.Count < totalNumbers) { int number = random.Next(); if (!uniqueNumbers.Contains(number)) { uniqueNumbers.Add(number); } }Rajesh GamiRank #30 · 41.5K reputationPosted Jan 16, 2023, 1:44 AM EST
List listNumbers = new List(); int number; for (int i = 0; i < 6; i++) { do { number = rand.Next(1, 49); } while (listNumbers.Contains(number)); listNumbers.Add(number); }You can create a function and add a min-max value that can generate a random number between those min max values
Vishal JoshiRank #377 · 6.7K reputationPosted Jan 16, 2023, 12:55 AM EST
Hello Naimish,
You can use the Random class to create random numbers. Please check below sample code.
Random rnd = new Random(); int month = rnd.Next(1, 13); // creates a number between 1 and 12Thanks
Jignesh KumarRank #91 · 22.9K reputationPosted Jan 16, 2023, 2:42 AM EST
you can use Random calss to generate random int number in various way,
Random rnd1 = new Random(); var uniqueNumber = new List(); for (int j = 0; j < 20; j++) { var num = rnd1.Next(); if (!uniqueNumber.Contains(num)) { uniqueNumber.Add(num); } }Note : Next(); has method overloading which will allow you to pass different parameter so you can use according to your requirement.
Sandhiya PriyaRank #26 · 43.2K reputationPosted Jan 6, 2026, 11:17 PM EST
you want to generate unique random numbers in C# so that once a number is picked, it won’t repeat.
Here are a few clean approaches:
Approach 1: Use a
HashSetto Track Generated Numbersusing System; using System.Collections.Generic; class Program { static void Main() { Random rand = new Random(); HashSet generated = new HashSet(); int min = 1, max = 100; // range int count = 10; // how many unique numbers you want while (generated.Count < count) { int num = rand.Next(min, max + 1); // inclusive range if (generated.Add(num)) // Add returns false if already exists { Console.WriteLine(num); } } } }Ensures uniqueness using
HashSet.Works for any range.
If you need all numbers in a range, this can get inefficient for large ranges.
Approach 2: Shuffle a List (Best for Full Range)
using System; using System.Collections.Generic; class Program { static void Main() { int min = 1, max = 100; List numbers = new List(); for (int i = min; i 0; i--) { int j = rand.Next(i + 1); (numbers[i], numbers[j]) = (numbers[j], numbers[i]); } // Now numbers are unique and randomized foreach (var num in numbers) Console.WriteLine(num); } }Generates all numbers in range without duplicates.
Efficient if you need a complete set of unique randoms.
Uses more memory for very large ranges.
Approach 3: LINQ One-Liner (Quick & Elegant)
using System; using System.Linq; class Program { static void Main() { Random rand = new Random(); var uniqueNumbers = Enumerable.Range(1, 100) .OrderBy(x => rand.Next()) .Take(10); foreach (var num in uniqueNumbers) Console.WriteLine(num); } }Concise.
Great for small sets.
Less efficient for huge ranges.
Practice
If you need all numbers in a range ? use Approach 2 (Shuffle).
If you need a few unique numbers ? use Approach 1 (HashSet).