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;
}

  

posted @ 2013-07-25 01:39  pgu2  阅读(274)  评论(0)    收藏  举报