Was interviewing

>was interviewing
>was asked to write a random integer generator between 0 and 6 given a random 0 1 generator
> never heard this question before
>can not figure it out by myself and had to ask for hints
>came up with an anwser that is acceptable after those hints are given finally but didn't have time to code
Am I a brainlet?

Attached: 1*vmGJG77e-nLnKlv-tUgf5w.jpg (800x533, 72K)

Other urls found in this thread:

redblobgames.com/articles/probability/damage-rolls.html
merriam-webster.com/dictionary/random
libgen.is/book/index.php?md5=EDF8B50556411915E32AFC152B2E7043
twitter.com/SFWRedditGifs

sum = 0
for i in 0 to 6:
sum += rng.get()
return sum

I tried doing this by myself once a mental exercise (obvious without just generating any random int and using modulo since that's cheating), and the fundamental problem I ran into was how I turn a 1/2 chance into a 1/3 chance or 1/5 or some other prime reciprocal. Is there a proper solution to this problem, or is it just or generating a large enough integer that the modulo method produces a fair enough distribution?

multiply it by 6 then floor it

so the answer is either 0 or 6... Jow Forumsenius
it's this and it's very obvious.

Think the 0 1 is implied to be integer

Math.floor(Math.random() * 7)

This will work but you won't get uniform distribution.
Simplest way to do that would be to roll 3 times for each bit and retry if >6. You could also do modulo instead but that'd make 0 and 1 more likely.

There is. You can step down your dice by retrying if the result is undesirable. If you do the math it makes all the other results equally more likely.
It makes your rng have an unbounded running time, though.

int n = 0;
n = (n