You have int rand1() which outputs 0 or 1 in equal probability.
You need to implement int randx(int x) which outputs 0,1,2,3,4,...,x.
It is easy to set bits based on rand1 output for functions like rand3 or rand7. For rand3, just call rand1 twice and set bit 0 and 1 based on the output and 00,01,10,11 will have equal probability to get select (25%). But how about functions like rand4?
Aucun commentaire:
Enregistrer un commentaire