random selection algorithm
frequency-based sampling
efficient algorithms
probabilistic selection
item selection algorithm

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.

Practice algorithms

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 ii has a frequency fif_i, then its selection probability PiP_i is given by:

Pi=fijfjP_i = \frac{f_i}{\sum_{j}f_j}

where jfj\sum_{j}f_j 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:

  1. 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] .
  2. 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.
  3. 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 O(n)O(n) to O(logn)O(\log n) 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 O(n)O(n) 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
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.