18
2
A friend has an add-on card in their computer which generates a perfectly random number from 1 to 5 inclusive. Unfortunately, they spilt cola on it somehow, and it now generates only 2's for all numbers from 1 to 4. Luckily the randomness is preserved, but 2 has a probability of 80% and 5 has a probability of 20%, and there are no 1's, 3's or 4's generated. Using this random source (call it BrokenRand()
or something similar), write a working random number generator which produces numbers from 1 to 5 each with an equal 20% probability with the same perfect randomness as the original source.
Shortest program wins. Bonus points awarded for the minimum number of calls to BrokenRand
impartially by a demographically-selected customer service focus consultancy, broken down by age and sex - i.e. me.
You could generate 7 bits and extract 3 random numbers to reduce the calls to BrokenRand, but that would likely cost a few more strokes – gnibbler – 2011-02-07T08:39:29.810
This is really well done! I like how you short circuit the increment value. – snmcdonald – 2011-02-05T14:03:43.990