Skip to main content
LeetCode 470, Medium. Topics: Math, Rejection Sampling, Randomized, Probability and Statistics. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 19 parametrized pytest cases, and a playground notebook:

Problem

Given the API rand7() that generates a uniform random integer in the range [1, 7], write a function rand10() that generates a uniform random integer in the range [1, 10]. You can only call the API rand7(), and you shouldn’t call any other API. Please do not use a language’s built-in random API. Each test case will have one internal argument n, the number of times that your implemented function rand10() will be called while testing. Note that this is not an argument passed to rand10().

Examples

Constraints

  • 1 <= n <= 10^5
Follow up:
  • What is the expected value for the number of calls to rand7() function?
  • Could you minimize the number of calls to rand7()?

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

Last modified on September 7, 2026