All about flooble | fun stuff | Get a free chatterbox | Free JavaScript | Avatars
 perplexus dot info

 More randomness! (Posted on 2003-06-30)
Suppose you have a function (or a magic ball) that is capable of producing a totally random integer between 1 and 5 (inclusive).

Using this, how would you generate a random number (also an integer) between 1 and 7 (inclusive)? (Note that the for the number to be random, all integers between 1 and 7 must have an equal chance of being generated)

Assume that using your 1-5 generator is pretty time-consuming, so you want to minimize the number of times you are going to use it.

 See The Solution Submitted by levik Rating: 4.3333 (6 votes)

Comments: ( Back to comment list | You must be logged in to post comments.)
 re(4): solution | Comment 12 of 16 |
(In reply to re(3): solution by friedlinguini)

If I'm interpreting your description correctly, the non-static version of your algorithm averages about 2.219 calls to rand5 for each base-7 digit. My algorithm averages 1.266 calls to rand5 per base-7 digit given that a large number of them will be needed. I take it that applying STATIC to your technique will actually bring it down to the theoretical 1.209 calls per digit; is that correct? (that's log(7)/log(5)).
 Posted by Charlie on 2003-07-01 05:38:24

 Search: Search body:
Forums (0)