Home All Groups Group Topic Archive Search About

Correct way to get random results with arrays

Author
25 May 2005 8:41 PM
Macsicarr
Hi All

Could you please help me with this because I am going round the bend.

In essence, I'm working with the following type of array:

arrNames(x,y)
x always equals 0 in this case because I'm working with 1 dim here
y is variable as the data I put in it is from an Access table, so this might
range from 0 to 5

All I want to do is get 2 random indexs from this array, but it always seems
to select 0 or 1 even if 'y' is set to 3, ie 0 to 3.

My random code is as follows:

bytNameSeed1 = Int(((UBound(arrNames, 2) - LBound(arrNames, 2)) * Rnd) +
LBound(arrNames, 2))

Do
  bytNameSeed2 = Int(((UBound(arrNames, 2) - LBound(arrNames, 2)) * Rnd) +
LBound(arrNames, 2))
Loop Until bytNameSeed2 <> bytNameSeed1

For some reason if y has say 3 values in it (ie 0 - 2) then the above always
manages to pick 0 and 1 or 1 and 0, but never 2.

What's worse is that if y has onlt 2 values (ie 0 - 1) then it might get the
first random value OK, ie 0 or 1, but to get the other one in the second
random run (ie 1 or 0) it appears to go in an infinite loop.

All I want is for it to work with the numbers that are available to it so
that:

a) if y has say 4 values (ie 0 - 3) then there is the possibility that it
will produce 2 random numbers out of the 4 available not just 0 and 1.

b) More importantly it won't go into an infinite loop if there are only 2
options available (ie 0 and 1).  I would have thought this smaller range
would have made my routine quicker, but it makes it worse.

Any ideas?

Author
25 May 2005 10:31 PM
Rick Rothstein
Show quote Hide quote
> Could you please help me with this because I am going round the bend.
>
> In essence, I'm working with the following type of array:
>
> arrNames(x,y)
> x always equals 0 in this case because I'm working with 1 dim here
> y is variable as the data I put in it is from an Access table, so this
might
> range from 0 to 5
>
> All I want to do is get 2 random indexs from this array, but it always
seems
> to select 0 or 1 even if 'y' is set to 3, ie 0 to 3.
>
> My random code is as follows:
>
>  bytNameSeed1 = Int(((UBound(arrNames, 2) - LBound(arrNames, 2)) *
Rnd) +
> LBound(arrNames, 2))
>
>  Do
>   bytNameSeed2 = Int(((UBound(arrNames, 2) - LBound(arrNames, 2)) *
Rnd) +
> LBound(arrNames, 2))
>  Loop Until bytNameSeed2 <> bytNameSeed1
>
> For some reason if y has say 3 values in it (ie 0 - 2) then the above
always
> manages to pick 0 and 1 or 1 and 0, but never 2.

According to VB help files for the Rnd function, the formula for getting
a random number within a range is this...

     Int((upperbound - lowerbound + 1) * Rnd + lowerbound)

Your formula appears to be missing the "+1" part. If you are always
using zero based arrays, you then the LBound is 0 and you can simply
remove it from the calculation. Also, if your first dimension is always
0 making it a one-dimensional array, why not use an actual
one-dimensional array....

     Dim arrNames()
     ReDim arrNames(5)
     bytNameSeed1 = Int((Ubound(arrNames) + 1) * Rnd)

Rick - MVP