random selection
number range
combinatorics
sampling methods
algorithms

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.

Practice algorithms

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:

  1. Each number should be unique.
  2. The selection should be random.
  3. `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.

  1. Initialize a Range: Create a list of numbers from `a` to `b`.
  2. Shuffle the List: Apply the Fisher-Yates shuffle to ensure randomness.
  3. Slice the List: Select the first `k` numbers from the shuffled list.

Complexity

  • Time Complexity: O(n)O(n), where `n` is the number of elements in the range `[a, b]`.
  • Space Complexity: O(n)O(n) 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.

  1. Initiate a Set: Begin with an empty set to automatically handle duplicate entries.
  2. 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: O(k)O(k) to O()O(\infty) in the worst case where continual duplicates cause excessive recomputations.
  • Space Complexity: O(k)O(k).

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: O(k)O(k).
  • Space Complexity: O(n)O(n) because the list is temporarily stored.

Related reading
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.