realbasic-nug
[Top] [All Lists]

Re: Checking array for double entries

To: REALbasic NUG <realbasic-nug@lists.realsoftware.com>
Subject: Re: Checking array for double entries
From: William Squires <wsquires@satx.rr.com>
Date: Sat, 28 Jun 2008 18:45:38 -0500
Authentication-results: mx.google.com; spf=neutral (google.com: 74.124.194.228 is neither permitted nor denied by best guess record for domain of realbasic-nug-bounces@lists.realsoftware.com) smtp.mail=realbasic-nug-bounces@lists.realsoftware.com
Delivered-to: listarchive@realsoftware.com
In-reply-to: <233017.99162.qm@web28006.mail.ukl.yahoo.com>
References: <233017.99162.qm@web28006.mail.ukl.yahoo.com>
Reply-to: REALbasic NUG <realbasic-nug@lists.realsoftware.com>
Sender: realbasic-nug-bounces@lists.realsoftware.com
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>


<Prev in Thread] Current Thread [Next in Thread>