Results 1 to 8 of 8

Thread: Random Numbers - Ensuring no duplicates

  1. #1

    Thread Starter
    Lively Member MileOut's Avatar
    Join Date
    Nov 2001
    Location
    Glasgow
    Posts
    83

    Random Numbers - Ensuring no duplicates

    I've been having problems generating a series of five random numbers that I store into an integer array. My problem is that I don't know how to ensure that each random number is unique and not a duplicate of another within the array.

    I'm trying this now with picking random numbers between 1 & 5 so at least that way, I can tell if I am getting duplicates within that range, in the hope that the array wil have store all numbers 1,2,3,4,5 just to prove that these numbers are chosen randomly, and without duplication.

    Currently, I'm getting 2, 2, 4, 5, 1 , or whatever.

    Any ideas or code out there that can help?

  2. #2
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    The only way i see to ensure a number isn't used yet is to loop through the used numbers and compare them with the randomly generated one.
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    if the limit of the random numbers is the same as the array (ie: elements 1-5 contain numbers 1-5 in random order), the best way by far is to fill the array in order, then randomly swap elements around.

  4. #4

    Thread Starter
    Lively Member MileOut's Avatar
    Join Date
    Nov 2001
    Location
    Glasgow
    Posts
    83
    I was just using 1 to 5 to test that it was not making duplicates.

    Essentially, I could have over 1000 and need to pick 5. I just don't want to run the risk of duplication...

    To be honest, though, I should have search before I posted as I've found an older thread that I've found to be extremely helpful.

    Thanks, anyway.

  5. #5
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Use the dictionary object.
    For each number generated, check if its already in the dictionary, and if not, then add to the dictionary and add the number to your array or list or whatever you're doing.

    That is the fastest and most efficient method of duplicate removal.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  6. #6
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695

    Thumbs up How about this...

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Dim i As Integer                    'Loop Counter
    5.     Dim usedNumbers(50) As Integer      'hold the used numbers
    6.     Dim CurrentNumber As Integer        'The current random number
    7.     Dim j As Integer                    'Loop Counter
    8.     Dim k As Integer                    'Holds our place in the UsedNumbers Array
    9.     Dim BadNumber As Boolean            'Is it a bad number?
    10.  
    11.     For i = 0 To 49
    12.         CurrentNumber = Int(Rnd() * 50) + 1
    13.         'Ensure that you get numbers between 1 and 50   Int(Rnd() * 50) + 1
    14.         For j = 0 To 49
    15.             'Check the new number against every other number we've generated
    16.             If (CurrentNumber = usedNumbers(j)) Then
    17.                 BadNumber = True
    18.                 Exit For
    19.             End If
    20.            
    21.             If (usedNumbers(j) = 0) Then
    22.                 'We will only have zeroes in the array if no value has been entered
    23.                 'in used Numbers for that index so we can safely leave if we see a zero
    24.                 Exit For
    25.             End If
    26.         Next j
    27.        
    28.         While BadNumber
    29.             'if its a bad number we need to keep generating randome numbers until we have  good one
    30.             BadNumber = False
    31.             CurrentNumber = Int(Rnd() * 50) + 1
    32.             For j = 0 To 49
    33.                 'Do the same check to see if it is a bad number
    34.                 If (CurrentNumber = usedNumbers(j)) Then
    35.                     BadNumber = True
    36.                     Exit For
    37.                 End If
    38.             Next j
    39.         Wend
    40.         'You won't get here unless you have a good number
    41.         usedNumbers(k) = CurrentNumber
    42.         k = k + 1
    43.        
    44.         Debug.Print CurrentNumber
    45.     Next i
    46. End Sub

    Cheers...

  7. #7
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478

    Cheers

    Wrack , using your code i'll cut it a bit too long for me.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Dim i As Integer                    'Loop Counter
    5.     Dim usedNumbers(50) As Integer      'hold the used numbers
    6.     Dim CurrentNumber As Integer        'The current random number
    7.     Dim j As Integer                    'Loop Counter
    8.  
    9.  
    10.     For i = 0 To 49
    11.         CurrentNumber = Int(Rnd() * 50) + 1
    12.         'Ensure that you get numbers between 1 and 50   Int(Rnd() * 50) + 1
    13.         For j = 0 To i ' only have to loop to the latest added
    14.             'Check the new number against every other number we've generated
    15.             If (CurrentNumber = usedNumbers(j)) Then
    16.                 j = i ' to get out of the inner for loop
    17.                 i = i -1 ' reset i to one lower cause it was a bad number
    18.             End If
    19.  
    20.           If j <= i Then  ' number we don't have yet
    21.              usedNumbers(i) = CurrentNumber
    22.           End If
    23.     Next i
    24. End Sub
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  8. #8
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    Here is an other option using a collection instead.
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim i As Integer
    3. Dim col As Collection
    4. Dim intIndex As Integer
    5. Dim intArr(4) As Integer
    6.  
    7. '   Create a collection to hold all the numbers
    8.     Set col = New Collection
    9.  
    10. '   Add the numbers to the collection
    11.     For i = 1 To 5 '<=== 5 is for demo only.
    12.         col.Add i
    13.     Next i
    14.    
    15.     For i = 0 To 4
    16.         Randomize
    17. '       Find a random index from the
    18. '       remaining collection memebers
    19.         intIndex = Int(Rnd() * col.Count) + 1
    20.        
    21. '       Add the item to the array
    22.         intArr(i) = col(intIndex)
    23.        
    24. '       Remove the item from the collection
    25. '       so it isn't used again
    26.         col.Remove intIndex
    27.     Next i
    28.    
    29. '   Print the results
    30.     Me.Cls
    31.     For i = 0 To 4
    32.         Me.Print intArr(i)
    33.     Next i
    34. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width