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.
Sub Random37() As Integer
Dim I as Integer
Dim SngJ as Single
SngJ = Rnd()
For I = 1 to 36
If SngJ >= 1/(2^I) then
Return (I)
Stop
End If
Next I
Return (37)
Stop
End Sub
|
Posted by Erik O.
on 2004-06-08 14:57:00 |