Implement Rand10() Using Rand7() - Problem
Generate Random Numbers with Limited Resources
You're working with a system that only provides a
The Challenge: You can only call
Key Requirements:
• Use only the given
• Generate truly uniform distribution for numbers 1-10
• Minimize the expected number of calls to
This is a classic rejection sampling problem that tests your understanding of probability theory and efficient random number generation.
You're working with a system that only provides a
rand7() function that generates uniform random integers from 1 to 7. Your task is to implement a rand10() function that generates uniform random integers from 1 to 10.The Challenge: You can only call
rand7() - no other random functions are allowed!Key Requirements:
• Use only the given
rand7() API• Generate truly uniform distribution for numbers 1-10
• Minimize the expected number of calls to
rand7()This is a classic rejection sampling problem that tests your understanding of probability theory and efficient random number generation.
Input & Output
example_1.py — Basic Usage
$
Input:
Call rand10() multiple times
›
Output:
Random integers from 1 to 10 with uniform distribution
💡 Note:
Each call to rand10() should return a value from 1-10 with equal 10% probability for each outcome. The internal calls to rand7() are hidden from the user.
example_2.py — Distribution Test
$
Input:
Generate 10,000 samples using rand10()
›
Output:
Each number 1-10 appears approximately 1,000 times
💡 Note:
With a large sample size, we can verify that our implementation creates a truly uniform distribution where each outcome has equal probability.
example_3.py — Edge Case Verification
$
Input:
Multiple consecutive calls to rand10()
›
Output:
Independent random values, no correlation between calls
💡 Note:
Each call to rand10() should be independent - the result of one call shouldn't influence subsequent calls. This tests the randomness property.
Visualization
Tap to expand
Understanding the Visualization
1
Combine Two Dice
Use (first_die - 1) × 7 + second_die to create 49 unique outcomes
2
Select Fair Range
Keep only outcomes 1-40 since 40 divides evenly by 10
3
Map to Target
Use modulo to map each group of 4 consecutive numbers to outputs 1-10
4
Reject and Retry
If we get 41-49, throw it away and start over
Key Takeaway
🎯 Key Insight: Create a larger uniform range, then use rejection sampling to map equal-sized chunks to your desired outputs. This guarantees perfect uniformity while minimizing expected function calls.
Time & Space Complexity
Time Complexity
O(1) expected
Expected 2.45 calls to rand7(), but worst case could be infinite (with probability approaching 0)
✓ Linear Growth
Space Complexity
O(1)
Only uses a few variables regardless of input size
✓ Linear Space
Constraints
-
You can only call the API
rand7() - You cannot call any other API or use built-in random functions
- The function should return integers in range [1, 10]
- The distribution must be perfectly uniform
- 1 ≤ n ≤ 105 (number of test calls)
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code