View Single Post
  #7   Report Post  
JE McGimpsey
 
Posts: n/a
Default

In article ,
Andrea Jones wrote:

I've run 122757 iterations using this macro and there is no bias towards high
or low values.


I may be wrong, and I'm sure I'll be corrected if so, but...

Given

Dim a As Long
Dim i As Long
Dim arr(0 To 50) As Long
For i = 1 to 1000000
a = Rnd() * 50
arr(a) = arr(a) + 1
Next i

for the million trials, the arr(1) through arr(49) will each tally
approximately 2% or 20,000. arr(0) and arr(50) will each tally 1% or
10,000, since the coercion to a Long integer is equally likely to round
up or down (ignoring any bias of the rounding algorithm at
x.5000000000000000).

You then add Seed2 - Int(Seed2). If Timer, which Seed2 is based on, is
unbiased, then that factor will return [0-1) with an average of 0.5 (but
see below).

Assuming that's the case, the expected calculated, coerced, values of
a-e are now 0 - 51 before the corrections for a-e 50, with fewer
zero's expected, and more 5x's. The correction for 51's will almost make
up for the reduced number due to splitting the rounding.

I also suspect that the assumption that Seed2 - Int(Seed2) is unbiased
is flawed for any particular run of the routine, assuming it takes
significantly less than a second - e.g., if the first Timer reading is
xx.70 and the whole process takes, say, 0.2 seconds, then the results of
repeatedly calculating the Seed2 - Int(Seed2) values will be between 0.7
and 0.9 which will bias a-e upward (i.e, no zeros will be produced), but
if the first timer reading occurs at xx.01 then the number of zeros will
be nearly as much as without the factor.

So I don't see what Seed2-Int(Seed2) does to give a "better" random
number than, say:

a = Int(Rnd() * 50) + 1

I should have mentioned that all the variables are declared
as integers


That would eliminate any effect of Seed2. Seed2 should probably be a
Single, Double, or Variant.

I just wanted to give the gist of the code rather than the whole
thing. When I ran this so that the average number of occurrences for each
number was 2455 (122757 iterations) the number 50 occurred 2369 times.


Without the Seed2 correction, the expected number of 50's is 1227.57.
The Seed2 bias significantly raises that number.