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

Home > Algorithms
Random Number Generator (Posted on 2004-06-08) Difficulty: 3 of 5
A particular random number generator returns positive integer n with probability 1/(2^n). (ie '1' with probability 1/2, '2' with probability 1/4, '3' with probability 1/8, etc.)

Using this random number generator, write an algorithm which chooses a random integer from 1 to 37 with equal probability.

See The Solution Submitted by Brian Smith    
Rating: 2.0000 (1 votes)

Comments: ( Back to comment list | You must be logged in to post comments.)
Solution solution, not sure if it's been posted already | Comment 26 of 27 |

It's quite simple.  We first produce an algorithm to produce random numbers ranged from 1 to 2^6-64 uniformly.

The generator produce 1 with 1/2 probability and not 1 with 1/2 probability.  So if it's 1, narrow down the range to 1 to 32 with 1/2 chance and 33 to 64 to 1/2 chance.  Now run the generator again and narrow down the range by half again.  We'll need to run the generator 6 times so that the range will be limited to one number.

Now this generator will return 1-64 uniformly randomly.  If the number returned is greater than 37, simply throw it away and run it again.  The result should be a random number between 1-37 with equal chance.


  Posted by Bon on 2004-08-09 13:59:21
Please log in:
Login:
Password:
Remember me:
Sign up! | Forgot password


Search:
Search body:
Forums (0)
Newest Problems
Random Problem
FAQ | About This Site
Site Statistics
New Comments (17)
Unsolved Problems
Top Rated Problems
This month's top
Most Commented On

Chatterbox:
Copyright © 2002 - 2024 by Animus Pactum Consulting. All rights reserved. Privacy Information