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.)
re: program (needed some revision...) | Comment 4 of 26 |
(In reply to This little program should do the trick... by Erik O.)

I think the program I gave in an earlier post weights the upper range of number a bit heavily.  Two slight modifications should do the trick:

First-Run through the shuffleing part at least twice to distribute the numbers more fairly,

Second-In the output, use the first number taken from the array as a pointer to the real number to be displayed.

The program should then look more like:

Dim I, J, K, IntArray(37), Temp As Integer
' Fill Integer Array with values from 1 to 37
For I = 1 To 37
    IntArray(I) = I
Next I
'Shuffle Array
For K = 1 to 3
    For I = 1 To 37
    J = Rnd()
    Temp = IntArray(I)
    IntArray(I) = IntArray(J)
    IntArray(J) = Temp
Next I
Next K
Print IntArray(IntArray(Rnd()))
Stop
End

Either of the modifications mentioned above should work fine independantly--using both is probably overkill.

 


  Posted by Erik O. on 2004-06-08 14:44:16
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 (1)
Unsolved Problems
Top Rated Problems
This month's top
Most Commented On

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