Compute rand7 using rand5
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The challenge of creating a function rand7() using rand5() is a classic problem in computer science that involves generating a uniform distribution over a range of integers using another random generator with a different range. In this case, we aim to generate a random integer from 1 to 7 using a function that can only generate random integers from 1 to 5. This problem is significant because it exemplifies techniques used in randomness and probability manipulation—skills that are essential in simulation, cryptographic applications, and algorithm design.
Technical Explanation
Understanding rand5()
rand5() is a given function that returns uniformly random integers between 1 and 5, inclusive. This means that each integer from 1 to 5 has an equal probability of of being chosen.
Building rand7() from rand5()
The goal is to construct rand7(), which outputs integers from 1 to 7 with equal likelihood. Given rand5() generates five possible outcomes, a single call to rand5() doesn't provide enough different outcomes to map 1-to-1 to 7 outcomes while maintaining uniformity. Therefore, we must use multiple calls to rand5() judiciously.
The core idea: Simulating a larger range
To effectively achieve this, we combine multiple calls to rand5() to simulate a larger range. Here are the main steps used in our implementation:
- Generate a number in a larger range: By calling
rand5()twice, we can simulate numbers from 1 to 25 (5 * (rand5() - 1) + rand5()), offering a 25-length space. We utilize this range because is a multiple. - Mapping to 1-7: We want to divide this 25-space into equally probable 7-length blocks. However, since , we have some leftover numbers beyond three complete 7-length blocks (a total of 21 numbers).
- Eliminating bias: To eliminate bias, if the generated number is in [1, 21], we map it straight to 1 through 7. If not, we discard it and re-call the function.
Implementation
Here is an implementation in pseudocode:
- Explanation: By iterating until the generated number is within the first 21, we ensure each of 1, 2, ..., 7 have an equal probability of .
Analysis and Discussion
Why does this work?
The rejection sampling technique causes the non-uniform probability space (from 22 to 25) to be sampled infrequently enough to not affect the uniformity for numbers 1 through 21. Essentially, we ignore the overflow that would introduce bias by sampling until we get a suitable outcome.
Expected Performance
The efficiency of this algorithm depends on the average number of rand5() calls required:
- Probability of success in one loop iteration: .
- Expected number of tries (geometric distribution): Given by where , yielding iterations on average.
Considerations and Limitations
- Tradeoff between randomness and efficiency: The method achieves true randomness. However, there is a calculable tradeoff between correctness and performance, as this method can take multiple loops at worst.
- In any practical large-scale implementation, pseudo-randomness suffices for generating single-time random integer sequences. However, mathematical rigor as discussed here is crucial.
Summary Table
Here's a table summarizing key points of this approach:
| Aspect | Details |
| Input Functionality | Generates random number between 1 and 5 using rand5(). |
| Output Functionality | Provides a random number between 1 and 7. |
| Technique | Combined rand5() calls to simulate more outcomes.
Mapped numbers to desired range using rejection sampling. |
| Success Probability | for each loop iteration. |
| Average Iterations | Approximately 1.19 iterations per valid output. |
| Complexity and Consideration | O(1) average case Trade-offs between unpredictability and performance. |
The detailed explanation above outlines the feasibility and elegance of generating a uniform random number range with an initially limited-functionality generator, highlighting essential concepts in computer science regarding randomness and probability distribution.

