Implement Rand10() Using Rand7() - Problem
Generate Random Numbers with Limited Resources

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
The Dice Expansion StrategyDie 1rand7()+Die 2rand7()=Virtual49-sided dieOutcomes 1-49Acceptance Zone (1-40): Map to 1-101-4→15-8→29-12→313-16→417-20→521-24→625-28→729-32→833-36→937-40→10Rejection Zone (41-49): Try Again41, 42, 43, 44, 45, 46, 47, 48, 49 → REJECT & RETRY🎯 This ensures each number 1-10 has exactly the same probability!
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)

n
2n
Linear Growth
Space Complexity
O(1)

Only uses a few variables regardless of input size

n
2n
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)
Asked in
Facebook 45 Google 38 Microsoft 32 Amazon 28
89.3K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen