PDA

Click to See Complete Forum and Search --> : Random Numbers


wheino
Dec 8th, 2000, 02:00 PM
When I start my program I want to create random numbers but when I enter the code it will always display the random numbers in the same order, when I exit and re-open the program. How do I make it so that everytime I relaunch the program it generates random numbers in a completely different order?

Thanks,

Will

YoungBuck
Dec 8th, 2000, 02:46 PM
Are you using the Rnd function?? If so you need to make a call to the Randomize function before hand to assure that you don't always get the same values....


Dim iCt as Integer
dim iArray(1 to 5) as Integer

For iCt = 1 to 5

Randomize
iArray(iCt) = rnd * 20

Next iCt

Gazz954
Dec 8th, 2000, 02:53 PM
I would prolly put the randomize function at the start of your code, you should only need to call it once to "seed" the random number generator.


Gazz

oetje
Dec 8th, 2000, 03:10 PM
I don't know the difference between

Randomize

And

Randomize Timer


Some people use Randomize Timer, I don't know why.:rolleyes:

YoungBuck
Dec 8th, 2000, 04:12 PM
Good point gazz here is the corrected code..


Dim iCt as Integer
dim iArray(1 to 5) as Integer

Randomize

For iCt = 1 to 5

iArray(iCt) = rnd * 20

Next iCt


Also there is no point in saying Randomize Timer because according to VB Help file if you omit the seed parameter the Timer is automatically used to generate the random seed


From the visual basic reference
Randomize Statement
Initializes the random-number generator.

Syntax

Randomize [number]

The optional number argument is a Variant or any valid numeric expression.

Remarks

Randomize uses number to initialize the Rnd function's random-number generator, giving it a new seed value. If you omit number, the value returned by the system timer is used as the new seed value.

If Randomize is not used, the Rnd function (with no arguments) uses the same number as a seed the first time it is called, and thereafter uses the last generated number as a seed value.

Note To repeat sequences of random numbers, call Rnd with a negative argument immediately before using Randomize with a numeric argument. Using Randomize with the same value for number does not repeat the previous sequence.

wheino
Dec 15th, 2000, 01:41 PM
Thanks for the help