The Implement Rand10() Using Rand7() coding problem is a probability and statistics challenge. You are given a library function rand7() that returns a uniform random integer in the range [1, 7]. You need to use this function to implement rand10(), which must return a uniform random integer in the range [1, 10].
Companies like Google and Yandex ask this to test a candidate's understanding of Rejection Sampling and probability distributions. It’s not just a coding problem; it's a math problem. It evaluations whether you can generate a larger uniform range from a smaller one without introducing bias (i.e., every number 1-10 must have an exact 10% chance of occurring).
This problem uses Rejection Sampling on a 2D coordinate space.
rand7() twice to generate a range from 1 to 49. The formula is (rand7() - 1) * 7 + rand7(). This creates a uniform distribution of 49 values.(num - 1) % 10 + 1.rand7() 2. Second call 5.(rand7() + rand7()) % 10 is NOT uniform. Some sums (like 7 or 8) occur much more frequently than others (like 2 or 14).num % 10. This makes 1-9 slightly more likely than 10 because they have 5 source values while 10 only has 4.rand7(), though basic rejection sampling is usually sufficient.Always emphasize Unbiasedness. In any "RandX to RandY" problem, the source range () must be larger than or equal to , and you must discard the "leftover" values that would prevent an even distribution.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Generate Random Point in a Circle | Medium | Solve | |
| Soup Servings | Medium | Solve | |
| Statistics from a Large Sample | Medium | Solve | |
| New 21 Game | Medium | Solve | |
| Shuffle an Array | Medium | Solve |