Randomly selecting k different numbers in a range
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
When faced with the problem of randomly selecting `k` different numbers from a defined range, it presents an interesting challenge that combines elements of probability, statistics, and computer science. This task is common in scenarios like random sampling and lottery number generation, where ensuring the uniqueness of each selected sample is important. Below, we explore various methods to achieve this and examine their strengths and potential drawbacks.
Conceptual Overview
To begin with, consider a range `[a, b]` from which we need to select `k` unique integers such that the output list is a random subset of that range. The primary conditions are:
- Each number should be unique.
- The selection should be random.
- `k` should be less than or equal to `(b - a + 1)`, ensuring there are enough numbers to choose from.
Methods for Random Selection
Method 1: Shuffle and Slice
This method utilizes the Fisher-Yates shuffle algorithm, also known as the Knuth shuffle, which provides a uniform random permutation.
- Initialize a Range: Create a list of numbers from `a` to `b`.
- Shuffle the List: Apply the Fisher-Yates shuffle to ensure randomness.
- Slice the List: Select the first `k` numbers from the shuffled list.
Complexity
- Time Complexity: , where `n` is the number of elements in the range `[a, b]`.
- Space Complexity: due to storing the list.
Method 2: Using a Set for Sampling
Consider using a set to ensure the uniqueness of elements while randomly generating numbers until the desired number of unique samples (`k`) is reached.
- Initiate a Set: Begin with an empty set to automatically handle duplicate entries.
- Random Sampling: Continue generating random numbers in the range `[a, b]` and add them to the set until its size equals `k`.
Complexity
- Time Complexity: to in the worst case where continual duplicates cause excessive recomputations.
- Space Complexity: .
Method 3: Random Sampling Without Replacement
Many programming languages have libraries or built-in functions to simplify this task. For instance, Python's `random.sample` method can be used:
- Time Complexity: .
- Space Complexity: because the list is temporarily stored.
Related reading
- Randomness in Artificial Intelligence Machine Learning
- Range Minimum Query On, O1 approach from tree to restricted RMQ
- Range Minimum Query On, O1 approach Last steps
- Rank items in an array using Python/NumPy, without sorting array twice
- Rasterizing a 2D polygon
- Ray-box Intersection Theory
- Ranking algorithm using likes / dislikes and average views per day
- Ranking algorithms

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.