Skip to content
Problem 470 · LeetCode 470(opens in a new tab)

Implement Rand10() Using Rand7()

MediumMath

You have a function rand7() that returns a whole number from 1 to 7, each with the same chance, and every call independent of the others. Using only rand7(), write rand10(), which returns a whole number from 1 to 10, each with the same chance.

You may not call any other source of randomness, such as your language’s built-in random functions. The tests call rand10() n times and check that the results look uniform.

Example 1

Input:  n = 1
Output: [4]
Why:    any one value from 1 to 10 is a correct output
2 more examples and the constraints

Example 2

Input:  n = 2
Output: [9, 1]
Why:    two independent values, each from 1 to 10

Example 3

Input:  n = 3
Output: [6, 6, 10]
Why:    repeats are fine; only the odds matter

Constraints

  • 1 ≤ n ≤ 100,000, where n is how many times the tests call rand10()
  • Follow-up: how many calls to rand7() does one rand10() make on average, and can you make that smaller?

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. A digit and a coinO(1) expected
  2. Rejection on a 7 × 7 gridO(1) expected
  3. Reuse the rejected rollsO(1) expected

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeAverage rand7() calls per rand10()Verdict
A digit and a coinO(1) expected
Correct; about 2.57 calls
Rejection on a 7 × 7 gridO(1) expected
Correct; about 2.45 calls
Reuse the rejected rollsO(1) expected
Best; about 2.19 calls

Every approach is constant time on average; the bar is the expected number of rand7() calls.

Edge cases to test

  • Adding two rolls. rand7() + rand7() gives 2 to 14 but not uniformly: 8 is seven times as likely as 2. Combine rolls as digits, not as a sum.
  • Using a modulo directly. rand7() % 10 never gives 8, 9 or 10, and 49 % 10 leaves 1 to 9 one time more likely. Reject the extra outcomes first.
  • Unbounded loops. The loop has no fixed limit, but each round ends with a fixed chance, so long runs are vanishingly rare.
  • Small n. With n = 1 any value from 1 to 10 is right. Uniformity is about the odds, which only many calls can show.

Common mistakes

  • Returning (rand7() * rand7()) % 10 + 1 or another formula whose outcomes are not equally likely.
  • Keeping 1 to 49 and taking % 10, which makes 1 to 9 more likely than 10.
  • Reusing a rejected roll as the first roll of the next try without the arithmetic that keeps it uniform.

How to talk through it

  1. 1Clarify. Is rand7() uniform and independent between calls? Do you care about the average number of calls?
  2. 2Explain the problem. 7 does not divide into 10, so a single roll cannot work; you need more outcomes than 10 and must drop the extra ones.
  3. 3Build the grid. Two rolls make 49 equal outcomes; keep 40, which map four to each value, and reroll the other 9.
  4. 4Answer the follow-up. That costs 2.45 calls on average. Recycling the rejected 1 to 9 and then 1 to 3 brings it to about 2.19.
  5. 5Test out loud. Call it many times and check that each value comes up close to one tenth of the time.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?