Generating a random, non-repeating sequence of all integers in .NET
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Generating a random, non-repeating sequence of integers can be useful in various applications, from games and simulations to sampling and shuffling datasets. In .NET, creating such sequences involves utilizing both the Random
class and collections like List
, HashSet
, or Queue
. This article provides a detailed exploration of generating these sequences efficiently in .NET, covering technical explanations, examples, and best practices.
Concept Overview
When generating a random sequence of integers that does not repeat any elements, there are a few primary strategies to consider:
- Shuffling Technique: This involves placing all integers in a collection and then shuffling the collection.
- Random Sampling: Select integers randomly and ensure each selection is unique by tracking selections.
- Permutations Generation: Generate all possible permutations and select one randomly.
Among these, the shuffling technique is commonly preferred for its simplicity and efficiency in most use cases.
Implementing in .NET
The Random
Class
The Random
class in .NET is essential for generating random numbers. An important aspect is ensuring that it uses a unique seed across instances to avoid identical number generation.
Shuffling Algorithm: Fisher-Yates
The Fisher-Yates shuffle, also known as the Knuth shuffle, is an efficient algorithm to generate a random permutation of a finite sequence. Here is an implementation using C#:
- The Fisher-Yates shuffle operates in time complexity, where is the number of integers. This makes it highly efficient for reasonably sized ranges.
- Memory usage is proportional to the size of the list as it must hold all integers.
- Re-seeding the Random Generator: For scenarios where different sequences are needed across runs, seed the
Randomwith a unique value, such as the current timestamp. - Extending Range Limits: The current implementation suits a bounded range. For dynamically extending ranges or larger datasets, consider more advanced data structures or parallel processing.
Related reading
- Generating a tower defense maze longest maze with limited walls - near-optimal heuristic?
- Generating all combinations of elements in a single array in pairs
- Generating All Combinations of List n Levels Deep in Java
- Generating all factors of a number given its prime factorization
- Generating Unique Numeric IDs using DateTime.Now.Ticks
- Generic List - moving an item within the list
- Generating all permutations excluding cyclic rotations
- Generating all permutations of a given string

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.