[Solution] rand5 | Two Sigma Interview Question
This problem was asked by Two Sigma.
Using a function rand7()
that returns an integer from 1
to 7
(inclusive) with uniform probability, implement a function rand5()
that returns an integer from 1
to 5
(inclusive).
Idea behind the solution
Similar to the fair coin problem, we just need to use the function rand7()
until we get something between 1
and 5
since we were told in advance that rand7()
is already supposed to be unbiased.
Please check the main.js snippet for the solution.
This solution originally posted at: Github by @Murillo2380
Comments
Leave a comment
You are not LoggedIn but you can comment as an anonymous user which requires manual approval. For better experience please Login.