Possible Duplicate:
Expand a random range from 1-5 to 1-7
I understood the solution using rejection sampling i.e
public static int rand7() {
while (true) {
int num = 5 * (rand5() - 1) + (rand5() - 1);
if (num < 21) return (num % 7 + 1);
}
}
but I am thinking of another solution, i.e rand5() is called 7 times and result is divided by 5, but I am not sure whether this is correct. Please let me know if is or isn't.
public static int rand7() {
int num = rand5()+rand5()+rand5()+rand5()+rand5()+rand5()+rand5();
return num/5;
}
EDIT: It looks like the probability of generating 1 is (1/5)^7 but to generate 2 it is 7*(1/5)^7. It is uneven so it is not going to work.
It will not be a uniform distribution (looks normal). And as Paul says, the proof follows from the Central Limit Theorem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With