Leetcode 470. Implement Rand10() Using Rand7()
The task is to implement a function int rand10()
that utilizes a given API function int rand7()
, which generates a uniform random integer in the range [1, 7]
, to generate a uniform random integer in the range [1, 10]
.
rand7()
generate truly random integers uniformly?
rand7()
?
rand7()
to improve performance. The solution should balance simplicity and efficiency.rand7()
can be called?
To use rand7()
to generate a uniform distribution from 1 to 10, one straightforward way is to consider generating numbers in the range [1, 49]
using two calls of rand7()
, as 7 * (rand7() - 1) + rand7()
covers ranges from 1 to 49 uniformly:
rand7()
to create a range from 1 to 49:
a = rand7() - 1
be an integer in [0, 6]
.b = rand7()
be an integer in [1, 7]
.val = a * 7 + b
, which produces an integer in [1, 49]
.If the generated integer val
is in the range [1, 40]
, use val % 10 + 1
to get a number in the range [1, 10]
.
val
is outside the range [1, 40]
, reject it and repeat the process (rejection sampling).This method ensures that each number between 1 and 10 has an equal probability of being selected.
#include <cstdlib>
// Given API
int rand7();
int rand10() {
int val;
do {
// Generate a uniform number in the range [1, 49]
int a = rand7() - 1;
int b = rand7();
val = a * 7 + b; // (a * 7) gives values in multiples of 7, then add b.
} while (val > 40); // Since we only need numbers in the range [1, 40]
return (val - 1) % 10 + 1; // Normalize [1, 40] to [1, 10]
}
The expected time complexity for this solution is O(1), although the actual number of iterations in the loop depends on the probability of generating values in the range [1, 40]
from [1, 49]
. Specifically, the loop runs an average of 1.225 times per call to rand10()
, since the probability of acceptance is 40/49.
Therefore, this solution is efficient both in terms of implementation complexity and performance.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?