# Get Rand10 from Rand7

![](https://1824821017-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M3rU5fVRA3qiBmK8Dbx%2F-M4koHjYpUb-VPQqQWwm%2F-M4koTSNtcGqArS2LwCL%2FScreen%20Shot%202020-04-12%20at%204.55.23%20PM.png?alt=media\&token=6000ca7d-a5a6-4666-a11b-c60b39531d5e)

```python
class Solution(SolBase):
    def rand10(self):
        while True:
            rand48 = self.rand7()-1 + (self.rand7()-1)*7
            if rand48 <= 39:
                return rand48 // 4 + 1
```

事实上，我们只用平凑近似于10的倍数的rand即可，所以我们弄出36 再取29 最后除以3也能得到类似的结果

```python
class Solution(SolBase):
    def rand10(self):
        while True:
            rand36 = self.rand7()-1 + (self.rand7()-1)*5
            if rand48 <= 29:
                return rand48 // 3 + 1
```
