Efficient algorithm to randomly select items with frequency
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
When handling large datasets or collections where items have different frequencies of occurrence, selecting items randomly with respect to their frequencies can offer a fair and efficient sampling mechanism. This method is commonly applied in simulations, gaming, data analysis, and in fields requiring weighted random selections.
Essentials of Frequency-based Random Selection
Problem Definition
Given a set of items, each associated with a frequency, the goal is to select items randomly in such a way that the probability of selecting an item is proportional to its frequency. This can be described mathematically as: If item has a frequency , then its selection probability is given by:
where is the sum of frequencies for all items.
Basic Approach: Cumulative Distribution
A foundational approach involves creating a cumulative distribution of items and using a random number to select an item. The steps for this approach are:
- Calculate Cumulative Frequencies: Construct an array where each element is the cumulative frequency up to that point. For example, if the input frequencies are
[3, 1, 4], the cumulative array will be[3, 4, 8]. - Generate a Random Number: Use a random number generator to pick a number within the total cumulative frequency, e.g., between 0 and 8 in the example above.
- Find the Interval: Determine which interval the random number falls into within the cumulative array to select the corresponding item.
Pseudocode Example
• Binary Search on the cumulative array can be used to find the interval containing the random number, improving efficiency from to for each selection. • Preparation: Construct two arrays, one for probabilities and one for aliases. This step redistributes frequencies. • Selection: Use a random number to decide which array to select from for a constant-time selection.
• Efficiency: Preprocessing allows for constant time complexity selection post setup in the alias method. • Flexibility: Adapts to situations where different weighting of items is required. • Preprocessing Time: Initial setup of alias tables requires effort and may not be ideal for dynamic, frequently changing datasets. • Numerical Stability: Handling floating-point arithmetic in cumulative sums requires careful attention to precision.
Related reading
- Efficient Algorithms for Computing a matrix times its transpose
- Efficient Array Storage for Binary Tree
- Efficient calculation of Fibonacci series
- Efficient Cartesian Product algorithm
- Efficient checking of whether a point is inside a large number of triangles in 2D
- Efficient combinations of N colored elements with restriction in the number of colors
- Efficient data structure for sparse data lookup
- Efficient Data Structure For Substring Search?

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.