## Algorithm

Problem Name: 470. Implement Rand10() Using Rand7()

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()`.

Example 1:

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

Example 2:

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

Example 3:

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

Constraints:

• `1 <= n <= 105`

## Code Examples

### #1 Code Example with Java Programming

```Code - Java Programming```

``````
class Solution extends SolBase {
public int rand10() {
int temp = 0;
while (true) {
temp = (rand7() - 1) * 7 + (rand7() - 1);
if (temp  <  40) {
break;
}
}
return temp % 10 + 1;
}
}
``````
Copy The Code &

Input

cmd
n = 1

Output

cmd
[2]

### #2 Code Example with Javascript Programming

```Code - Javascript Programming```

``````
const rand10 = function() {
let result = 40
while (result >= 40) {
result = 7 * (rand7() - 1) + (rand7() - 1)
}
return (result % 10) + 1
}
``````
Copy The Code &

Input

cmd
n = 1

Output

cmd
[2]

### #3 Code Example with Python Programming

```Code - Python Programming```

``````
class Solution:
def rand10(self):
return sum(rand7() for _ in range(10)) % 10 + 1
``````
Copy The Code &

Input

cmd
n = 2

Output

cmd
[2,8]

### #4 Code Example with C# Programming

```Code - C# Programming```

``````
using System;

namespace LeetCode
{
/**
* The Rand7() API is already defined in the parent class SolBase.
* public int Rand7();
* @return a random integer in the range 1 to 7
*/
public class _0470_ImplementRand10UsingRand7 : Rand7Base
{
public int Rand10()
{
int r = 40;

while (r >= 40)
r = (Rand7() - 1) * 7 + Rand7() - 1;

return r % 10 + 1;
}
}

public class Rand7Base
{
private readonly Random random = new Random();

public int Rand7()
{
return random.Next(7) + 1;
}
}
}
``````
Copy The Code &

Input

cmd
n = 2

Output

cmd
[2,8]