|
-
Aug 11th, 2002, 01:20 PM
#1
Thread Starter
Lively Member
Generating Random numbers, but not repeating any, is there an easy way to do this?
How can I generate random numbers but not repeat any that have already been generated. Is there an easy way to do this at all?
Thanks!
Eric
-
Aug 11th, 2002, 01:30 PM
#2
So Unbanned
Add the numbers you have generated randomly into a string, then use instr to check if that number has been generated, if so, randomly generate a new one.
VB Code:
10
x= int(rnd*255)
if instr(usednums,x) > 0 then
goto 10
else
usednums = x & "-" & usednums
end if
-
Aug 12th, 2002, 04:16 PM
#3
Fanatic Member
If you know the range of numbers you want the random numbers to be generated between you can just add all those values to an array and shuffle them around like a deck of cards and then select a new random number by moving to next item in the array. For example...
VB Code:
Private Sub ShuffleArray(ByRef arr(), ByVal iShuffleTimes As Integer)
Dim iCt As Integer
Dim iRandElem As Integer
Dim vTemp
'assure we don't get the same set of random numbers
Randomize
For iCt = 1 To iShuffleTimes
iRandElem = (Rnd * (UBound(arr) - LBound(arr))) + LBound(arr) ' get an element within the bounds of the array randomly
vTemp = arr(LBound(arr)) ' assign the value of the first element in the array to the temporary variable
arr(LBound(arr)) = arr(iRandElem) ' shuffle the values
arr(iRandElem) = vTemp
Next iCt
End Sub
Private Sub Form_Load()
Dim arr()
Dim i As Integer
Dim str As String
ReDim arr(1 To 1000)
' load the values 1-1000 into an array
For i = 1 To 1000
arr(i) = i
Next i
' shuffle there positions in the array around a bit
ShuffleArray arr, 3000
' get the first 10 numbers, guaranteed not to be repeating
For i = 1 To 10
str = str & arr(i) & vbCrLf
Next i
' display the 10 random numbers
MsgBox "Here are 10 random numbers " & vbCrLf & str
End Sub
-
Nov 27th, 2005, 02:31 AM
#4
Lively Member
Re: Generating Random numbers, but not repeating any, is there an easy way to do this?
 Originally Posted by DiGiTaIErRoR
Add the numbers you have generated randomly into a string, then use instr to check if that number has been generated, if so, randomly generate a new one.
VB Code:
10
x= int(rnd*255)
if instr(usednums,x) > 0 then
goto 10
else
usednums = x & "-" & usednums
end if
i know this is old but could someone elaborate on this post. It seems to me if the variable "usednums" holds the value of the generated number and the instr() checks to see if it matches the number that was generated, wouldnt it always go back to "10" on the goto loop? thanks
--- Science does not explain why things are what they are. What we get from Science is our interpretation of how things do what they do.
--- No Scientific law of the universe is stable, we did not create it, and we will never understand all of its abilities.
--- What we determine as reality is a mere assumption of what tomorrow will be based on what yesterday was.
-
Nov 27th, 2005, 02:51 AM
#5
Frenzied Member
Re: Generating Random numbers, but not repeating any, is there an easy way to do this?
digital error created another error here.
say we want to randomize 20 numbers, 1-20.
wouldn't instr(usednums,x) point to the digit 1 that is in 10 thru 19 if x=1?
-
Nov 27th, 2005, 03:32 AM
#6
Re: Generating Random numbers, but not repeating any, is there an easy way to do this
In VB, a random number is between 0 and .9999, so you have to multiply it to get a range of numbers. If you check for the closest Integer, you can get a number between 0 and the highest number -1.
if you say this, you get this
and if you don't want 0, you have to add 1, so you use this:
-
Nov 27th, 2005, 05:11 AM
#7
Re: Generating Random numbers, but not repeating any, is there an easy way to do this?
eprunner06,
Each time you generate a number add it to a collection. A collection will keep them unique.
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
|