Select k random elements from a list whose elements have weights
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
In many algorithmic problems, selecting random elements from a list with specified weights is a common task. Unlike uniform random selection, weighted random selection allows for some elements to be preferred over others based on assigned weights. This technique is important in areas such as machine learning, simulation, gaming strategies, and optimization tasks where randomness with specific biases is required.
Understanding Weighted Random Selection
Concept
Weighted random selection involves using a list of weights to adjust the probability of selecting each element. Put simply, elements with higher weights have a higher probability of being chosen than elements with lower weights. The selection process can be visualized as a "weighted lottery."
Probability Distribution
Assume a list with `n` elements denoted by , with corresponding weights . The probability of selecting element is:
This assures that elements are chosen based on their relative weights.
Algorithm Implementation
- Cumulative Weights: Calculate the cumulative sum of the weights.
- Random Number: Generate a random number in the range [0, total of all weights).
- Binary Search: Use binary search to find the largest index where the cumulative weight is greater than the generated random number. The element at this index is the selected one.
Example Code
Python is a commonly used language for implementing such algorithms due to its efficient libraries and readability. Here is an example using the `random` library for selecting a single element based on weights:
• Time Complexity: The algorithm runs in time, where is for constructing the cumulative sum and is for binary searching. • Space Complexity: for storing cumulative weights. • Simply repeat the single selection `k` times. • Code snippet:
• Adjust weights dynamically or use a more advanced data structure like a priority queue or reservoir sampling. • Normalization: If weights are not in the desired range, normalize them by the sum of weights. • Precision: Be mindful of floating-point precision errors especially in languages that do not handle large values smoothly. • Alternative Approaches: Algorithms like the Alias Method allow for constant time selection after an preprocessing.
Related reading
- Select N random elements from a List efficiently without toArray and change the list
- Select n records at random from a set of N
- Select top N elements of related objects
- Selecting such vector elements so that the sum of elements is exactly equal to the specified value
- SELECT Specific Value from map
- Selecting a range of items inside an array in C
- Selection algorithms on sorted matrix
- Separate the alphabet and digit such that their relative order remains the same in On time and O1 space

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.