Expand a random range from 1–5 to 1–7
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
Expanding a random range from 1 to 5 to a new range from 1 to 7 might seem straightforward, but it presents interesting challenges and solutions when considering probability distributions and unbiased transformations. This article delves into the technical methodologies by which this transformation can be achieved, ensuring each number in the new range is selected with equal probability.
Understanding the Problem
When expanding a range in random number generation, our primary goal ensures that each number in the expanded target range has an equal chance of being selected. For instance, if we roll a die that yields values from 1 to 5 and want to extend this to a die that yields values from 1 to 7, each number (1 to 7) should have a probability of occurring.
Techniques for Expanding the Range
1. Rejection Sampling
Rejection sampling is one effective method to achieve this expansion. By leveraging the existing 1-to-5 generator, we can craft a process that yields a uniform random number from 1 to 7. Here's how it works:
- Generate Two Numbers: Utilize the original random generator to produce two numbers, and .
- Calculate the Intermediate Result: Use the formula . The variable now has a range from 1 to 25.
- Map to New Range: Accept only if it falls within the first 21 numbers (i.e. ). For , reject it and repeat the process.
- Transform to Target Range: If is accepted, compute . This transforms to a new number between 1 and 7 with equal probability.
2. Probability Analysis
Each subset of 21 possible values represents a complete cycle in the modulo operation. The rejection sampling ensures that numbers from 22 to 25 don't introduce bias since they're disregarded and replaced with another try. Hence, the algorithm maintains a uniform distribution:
- Accepted Values: 1 to 21, divided evenly by 7.
- Probability Verification: Each of the seven numbers occurs three times out of the 21 accepted numbers, i.e., equals .
Implementation Example
Below is a simple Python example of implementing the above strategy with comments:
Key Points Summary
| Step | Action/Description |
| Initial Range | Use random from 1 to 5 |
| Extend to Range | Extend logic to reach 1 to 25 |
| Acceptable Output | Accept values in 1 to 21 |
| Final Range Mapping | Transform 1 to 21 to 1 to 7 evenly |
| Probability Outcome | Uniform distribution over new range |
Conclusion
Extending a random number generator from a smaller range to a larger one while maintaining uniformity in probability distribution involves creative use of existing generators and principles like rejection sampling. The outlined method reliably expands a range from 1 to 5 to 1 to 7 without bias, keeping choice probabilities consistent. This technique is useful in various computational applications, particularly in simulation and modeling, where randomness must be controlled with precision.
Related reading
- Explain how recursion works in an algorithm to determine depth of binary tree?
- Explain markov-chain algorithm in layman's terms
- Explain the implementation of Euler's Totient Implementation
- Explain this On log n algorithm for the Cat/Egg Throwing Problem
- Expectation Maximization coin toss examples
- Expectation Maximization coin toss examples
- Explain this snippet which finds the maximum of two integers without using if-else or any other comparison operator?
- Explain using xor to find two non-duplicate integers in an array

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.