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
Re: Generating Random numbers, but not repeating any, is there an easy way to do this?
Quote:
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
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?
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.
Quote:
.9999 * 2 = 1.9998
if you say this, you get this
Quote:
Int(.9999 * 2) = 1
and if you don't want 0, you have to add 1, so you use this:
Quote:
Int(.9999 * 2) +1 = 2
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.