> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Implement Rand10() Using Rand7()

> Tested Python solution for LeetCode 470 with 19 pytest cases. Generate a practice environment with lcpy.

LeetCode 470, [Medium](/catalog/medium). Topics: [Math](/catalog/topics/math), Rejection Sampling, Randomized, Probability and Statistics. [View on LeetCode](https://leetcode.com/problems/implement-rand10-using-rand7/description/).

Generate this problem as a practice environment: tested reference solution, 19 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 470   # by problem number
lcpy gen -s implement_rand10_using_rand7   # by problem name
```

## 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

```
Input: n = 1
Output: [2]
```

```
Input: n = 2
Output: [2,8]
```

```
Input: n = 3
Output: [3,8,10]
```

### 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](https://github.com/wislertt/leetcode-py/blob/main/leetcode/implement_rand10_using_rand7/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/implement_rand10_using_rand7/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
import random


# LeetCode exposes rand7() as a black-box API. It is modelled here as a
# concrete seeded class so the generated tests are deterministic and can also
# count how many rand7() calls each rand10() draw consumed.
class Rand7:
    def __init__(self, seed: int) -> None:
        self._rng = random.Random(seed)
        self.calls = 0

    def rand7(self) -> int:
        self.calls += 1
        return self._rng.randint(1, 7)


class Solution:
    # Rejection sampling on a 7x7 grid: 49 equally likely outcomes, 40 of them
    # map onto [1, 10] and the remaining 9 are discarded and redrawn. Expected
    # rand7() calls per rand10() is 2 * 49 / 40 ~= 2.45.
    # Time: O(1) expected per rand10() call
    # Space: O(1)
    def __init__(self, rand7_api: Rand7) -> None:
        self._rand7_api = rand7_api

    def rand10(self) -> int:
        while True:
            row = self._rand7_api.rand7()
            col = self._rand7_api.rand7()
            idx = (row - 1) * 7 + col
            if idx <= 40:
                return (idx - 1) % 10 + 1
```

## Complexity

| Time | Space |
| - | - |
| O(1) expected per rand10() call | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.