|
-
Mar 27th, 2000, 01:21 PM
#1
Thread Starter
New Member
I've got a small app that generates random numbers, but I don't want it to select the same number twice.
Can anyone help out with code.
Cheers.
-
Mar 27th, 2000, 01:33 PM
#2
Hyperactive Member
Then your not really talking about a Random number if it can't pick the same one twice 
But honestly. What you need to do is make a list for each of the numbers you have to choose from.
If you decide the random number can be from 1 to 100 then you need to be able to keep track of 100 numbers in a list!!!!
But here is a sneaky solution that you might find quite interesting... It is probably slightly LESS memory intensive than other solutions but a tiny bit heavier on the processing side, but it DOES avoid the need for arrays, collections and the like (which get tiresome)
Code:
' Set up the Variables
Dim sNumbers as String ' Keeps track of what you have used
Dim iMaxNumber as Integer ' Highest number possible
Dim iNum as Integer ' Iteration through your set
Dim iRand as Integer ' The number picked this time around
Dim sMsg as String ' Used to return the list
iMaxNumber = 100
sNumbers = String("0",iMaxNumber) ' Clear the list
For iNum = 1 to 10 ' Generate 10 random numbers
iRand = Int(Rnd * iMaxNumber)+1 ' Have a guess
Do While Mid(sNumbers,iRand,1) <> "0" ' If you already picked it
iRand = Int(Rnd * iMaxNumber)+1 ' Pick again
Loop ' Keep doing it until you get a new one
Mid(sNumbers,iRand,1) = "1" ' Mark this number off as being used
sMsg = sMsg & "- Random Number (" & iNum & ") = " & iRand & vbCR ' Add it to the list
Next iNum
MsgBox "Your Random Numbers : " & vbCR & vbCR & sMsg
But don't forget to reset the list all back to "0" again before using it a second time (unless you want to still pick new ones). Also remember that the more numbers you have chosen the slower it will be in finding another one you haven't chosen.
The alternative to speed this up is to create a collection with all the numbers in it as members and as you select the remaining members to delete that one... but it takes more space.
-
Mar 27th, 2000, 01:38 PM
#3
Hyperactive Member
Ah what the heck... here is the collection one as well :
Code:
Dim cNumbers as New Collection
Dim iMaxNumber as Integer
Dim iNum as Integer
Dim iRand as Integer
Dim sMsg as String
iMaxNumber = 100
For iNum = 1 to iMaxNumber
cNumbers.Add iNum
Next iNum
For iNum = 1 to 10 ' Still picking 10 numbers
If cNumbers.Count > 0 Then
iRand = Int(Rnd * cNumbers.Count) + 1
sMsg = sMsg & "- Random Number (" & iNum & ") = " & cNumbers(iRand)
cNumbers(iRand).Remove
End If
Next iNum
MsgBox "Random Numbers : " & vbCR & vbCR & sMsg
Now with this one you can keep pulling out numbers until the collection is reduced to nothing, it will work just as fast for the first number as it will for the last number but it takes a WHOLE lot more memory and the more numbers you want to choose from the more memory it will use.
Your choice ;P
-
Mar 27th, 2000, 01:38 PM
#4
Thread Starter
New Member
I'll give it a go. Thanks.
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
|