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

 Random Number Generator (Posted on 2004-06-08)
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.)
 This little program should do the trick... | Comment 3 of 26 |

In Visual Basic (yeah I know it's not a real programming language...):

`Dim I, J, IntArray(37), Temp As Integer`
`' Fill Integer Array with values from 1 to 37For I = 1 To 37    IntArray(I) = INext I`
`'Shuffle ArrayFor I = 1 To 37    J = Rnd()    Temp = IntArray(I)    IntArray(I) = IntArray(J)    IntArray(J) = TempNext I`
`Print IntArray(Rnd())`
`StopEnd`

 Posted by Erik O. on 2004-06-08 14:22:25

 Search: Search body:
Forums (0)