Rejection Sampling
Given a function which generates a random integer in the range 1 to 7, write a function which generates a random integer in the range 1 to 10 uniformly.
solution:
int rand10() {
int row, col, idx;
do {
row = rand7();
col = rand7();
idx = col + (row-1)*7;
} while (idx > 40); //reject sample here
return 1 + (idx-1)%10;
}
浙公网安备 33010602011771号