1 2 3 4 5 6 7 8 9 10 11
| class Solution extends SolBase { public int rand10() { while (true) { int randomValue = (rand7() - 1) * 7 + (rand7() - 1); if (randomValue < 40) { return randomValue % 10 + 1; } } } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| class Solution extends SolBase { public int rand10() {
int x; do { x = rand7() * 7 + rand7(); } while (!(x >= 11 && x <= 50));
return x % 10 + 1; } }
|
References
470. Implement Rand10() Using Rand7()