Given a function rand5() that returns integers from 0 to 5 with equal probability, write a function that returns integers from 0 to 7 with equal probability using rand5() only. .
Soluation : you need to generate 000 to 111 with equal probability.
int rand5()
{
[01,2,3,4,5]
}
int rand2() // it will return 0,1 with equal probability
{
rand5()%2
}
// rand2*4+rand2*2+rand2*1
int rand7(){
return rand2()*4 + rand2() * 2 + rand2()
<2 nbsp="" rand2="" span="">2>
<2 nbsp="" rand2="" span="">2>
}
each no with probability 1/8 (1/2*1/2*1/2)
each no with probability 1/8 (1/2*1/2*1/2)
No comments:
Post a Comment