|
-
Nov 7th, 2002, 04:58 AM
#1
Thread Starter
Lively Member
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?
-
Nov 7th, 2002, 05:07 AM
#2
Frenzied Member
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
-
Nov 7th, 2002, 05:23 AM
#3
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.
-
Nov 7th, 2002, 05:43 AM
#4
Thread Starter
Lively Member
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.
-
Nov 7th, 2002, 06:15 AM
#5
Retired VBF Adm1nistrator
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]
-
Nov 7th, 2002, 06:19 AM
#6
^:^...ANGEL...^:^
How about this...
VB Code:
Option Explicit
Private Sub Form_Load()
Dim i As Integer 'Loop Counter
Dim usedNumbers(50) As Integer 'hold the used numbers
Dim CurrentNumber As Integer 'The current random number
Dim j As Integer 'Loop Counter
Dim k As Integer 'Holds our place in the UsedNumbers Array
Dim BadNumber As Boolean 'Is it a bad number?
For i = 0 To 49
CurrentNumber = Int(Rnd() * 50) + 1
'Ensure that you get numbers between 1 and 50 Int(Rnd() * 50) + 1
For j = 0 To 49
'Check the new number against every other number we've generated
If (CurrentNumber = usedNumbers(j)) Then
BadNumber = True
Exit For
End If
If (usedNumbers(j) = 0) Then
'We will only have zeroes in the array if no value has been entered
'in used Numbers for that index so we can safely leave if we see a zero
Exit For
End If
Next j
While BadNumber
'if its a bad number we need to keep generating randome numbers until we have good one
BadNumber = False
CurrentNumber = Int(Rnd() * 50) + 1
For j = 0 To 49
'Do the same check to see if it is a bad number
If (CurrentNumber = usedNumbers(j)) Then
BadNumber = True
Exit For
End If
Next j
Wend
'You won't get here unless you have a good number
usedNumbers(k) = CurrentNumber
k = k + 1
Debug.Print CurrentNumber
Next i
End Sub
Cheers...
-
Nov 7th, 2002, 06:41 AM
#7
Frenzied Member
Cheers
Wrack , using your code i'll cut it a bit too long for me.
VB Code:
Option Explicit
Private Sub Form_Load()
Dim i As Integer 'Loop Counter
Dim usedNumbers(50) As Integer 'hold the used numbers
Dim CurrentNumber As Integer 'The current random number
Dim j As Integer 'Loop Counter
For i = 0 To 49
CurrentNumber = Int(Rnd() * 50) + 1
'Ensure that you get numbers between 1 and 50 Int(Rnd() * 50) + 1
For j = 0 To i ' only have to loop to the latest added
'Check the new number against every other number we've generated
If (CurrentNumber = usedNumbers(j)) Then
j = i ' to get out of the inner for loop
i = i -1 ' reset i to one lower cause it was a bad number
End If
If j <= i Then ' number we don't have yet
usedNumbers(i) = CurrentNumber
End If
Next i
End Sub
Code:
If Question = Incomplete Then
AnswerNextOne
Else
ReplyIfKnown
End If
cu Swatty
-
Nov 7th, 2002, 08:23 AM
#8
Here is an other option using a collection instead.
VB Code:
Private Sub Command1_Click()
Dim i As Integer
Dim col As Collection
Dim intIndex As Integer
Dim intArr(4) As Integer
' Create a collection to hold all the numbers
Set col = New Collection
' Add the numbers to the collection
For i = 1 To 5 '<=== 5 is for demo only.
col.Add i
Next i
For i = 0 To 4
Randomize
' Find a random index from the
' remaining collection memebers
intIndex = Int(Rnd() * col.Count) + 1
' Add the item to the array
intArr(i) = col(intIndex)
' Remove the item from the collection
' so it isn't used again
col.Remove intIndex
Next i
' Print the results
Me.Cls
For i = 0 To 4
Me.Print intArr(i)
Next i
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|