There are two easy solutions to the problem:
1) Use a dictionary and make the 'keys' the CStr() of the number you
want to check (with <dictionary>.HasKey(key) = True) If so, the
number has already been generated, and you can skip adding it to your
list or array or whatever.
2) Shuffling. Pre-generate an array with all the numbers (1..70) that
you want, then use a Bubblesort with the comparison replaced with a
random coin-flip. That way, you're guaranteed that you won't get a
duplicate since none existed in the first place (unless you have a
bug in your bubblesort routine, anyway... )
FYI, here the 'randomized bubblesort' routine
Sub RandomizeBubblesort(theData() As Integer)
// Assumes that UBound(theData) - 1 is the index of the last
element, and the indexing is 0-based
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim l As Integer
Dim swap As Integer
Dim shuffles As Integer
l = UBound(theData)
If (l > 0) Then
// make sure there really are elements in the array!
l = l - 1
// Shuffle 7 times (I read somewhere that shuffling is most
effective if done 7 times.)
For shuffles = 1 To 7
// Bubblesort stuff
For i = 0 To l - 1
For j = i + 1 To l
// But instead of comparing theData(i) with theData(j),
decide randomly whether
// to swap the elements.
// Note: don't get clever and put "If )Floor(Rnd * 100)
Mod 2_ Then", or you'll get a compiler
// warning as Floor() returns a double, but the Mod
operator only works on Integer data! We
// coerce it by assigning to k first, then we can do the
modulus! Best case - you don't get
// any warning/error, but the behavior is non-random (i.e.
it reverse sorts the numbers -
// yikes!)
k = Floor(Rnd * 100)
If k Mod 2 Then
swap = theData(i)
theData(i) = theData(j)
theData(j) = swap
End If
Next
Next
Next
End If
End Sub
On Jun 28, 2008, at 2: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>
|