Consider the more general problem of choosing k unique values from the
set 1 ... N.
In your case N is 70 and k is 6. Both of these values are quite small
so the choice of the solution is not too critical.
I think there are two general approaches to the problem (lots of
variants of course).
First approach is to generate the k values and check that you do not
get a duplicate. If you get a duplicate you generate another value
(until it is not a duplicate). A dictionary can be used to store
which values you have already generated. The pseudocode would look
something like the following.
for i = 1 to k
r = getRandom(1, N)
while Dictionary.HasKey(r)
r = getRandomInteger(1, N)
wend
RandArray(i) = r
Dictionary.value(r) = r // value stored in dictionary does not
matter
next
This approach works best if N is much larger than k since there will
not be many duplicates generated. If k is close to the value of N
then there are lots of duplicates and the method becomes slow. In
that case the following approach can be used.
dim values(N) as integer
for ndx = 1 to N // Fill an array with the values 1 .. N
values(ndx) = ndx
next
for ndx=1 to k
r = getRandomInteger( ndx, N) // r is between ndx and N
swap values(r) with values(ndx)
next
After k passes through the above loop, the first k entries of values
will be your randomly generated values.
This uses the least number of random numbers (which can be slow) but
does require the array to be filled with values 1..N.
I know that there were lots of other solutions posted but I didn't
think that they explained when you should choose the solution. Again,
for your case of 6 items from 70 it doesn't really matter because it
is fast and doesn't use much storage regardless of the solution you
pick.
Cheers,
Malcolm Smith
On Jun 28, 2008, at 12:22 PM, tobieichner77-rb@yahoo.de wrote:
Hello,
I wrote a small app that generates six random numbers out of a range
from 1 to 70. The requirement is that no single number is used twice.
So far the only solution I came upon to prevent double entries is to
compare all entries with each other. In the case of two equal
entries, I simply re-create the random array again and do another
check.
For example:
while x(1) = x(2) or x(1) = x(3) or x(1) = x(4) or x(1) = x(5) or
x(1) = x(6) or x(2) = x(3) or x(2)= x(4) or x(2) = x(5) or x(2)=
x(6) or x(3)= x(4) or x(3) = x(5) or x(3) = x(6) or x(4) = x(5) or
x(4) = x(6) or x(5) = x(6)
for y = 1 to 6 step 1
x(y) = r.inrange(1,70)
next
wend
As you see, this leads to a long and ugly "or" chain, which will
finally end into a total mess if there are, let's say, twenty
numbers to compare with each other.
Can you give me a hint how I can manage this easier and more
convenient ?
Bye,
Tobias.
__________________________________________________________
Gesendet von Yahoo! Mail.
Dem pfiffigeren Posteingang.
http://de.overview.mail.yahoo.com
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>
|