This Question is asked in different formats like simulate rand7() using rand5() function.
There is no right answer to this question if interviewer specifies that 7 sided die should have equal probability to al the numbers on it(i.e. 1-7). This is because 5 and 7 are coprime and there is no direct mapping available between coprimes.
There are several solutions which creates a biased 7 sided die.
Sol1:
Throw 5 sided die 7 times. sum will be between 7 and 35, with equal probable numbers. Then take the module 7 of the sum and add 1 to it.
This solution will produce biased die because probability of 1 is higher here.
Sol2:
rand7() = ((ran5() + rand5() + rand5() + rand5() + rand5() - 5) % 7) + 1
Categories
FollowersBlog Archive
Jobs
Find more freelance jobs
|
Saturday, January 10, 2009
Subscribe to:
Posts (Atom)
|
Popular Posts
|