Page 1 of 1

Repeating Random Numbers

Posted: Wed Mar 19, 2008 6:31 pm
by DDastardly71
I need help with Basic's pseudo-random numbers. I'm trying to generate a sequence of random numbers from a seed.

For example, if InitSeed = 745, I would like to be able to generate the same sequence of number every cycle of the loop.

InitSeed% = 745

FOR i% = 1 TO 3
FOR j% = 1 TO 5
RANDOMIZE InitSeed%

Num% = INT(100 * (RND + 1))
PRINT Num%
NEXT j%
NEXT i%

If the sequence were 55, 75, 33, 83, 18..... I would like the output to be this sequence printed 3 times using the InitSeed.

I've only been able to get the desired output IF I run one loop, re-start the program, and run it again. But if it's part of a running program, RANDOMIZING the InitSeed has no effect.

Re: Repeating Random Numbers

Posted: Thu Mar 20, 2008 8:56 am
by Mac
DDastardly71 wrote:If the sequence were 55, 75, 33, 83, 18..... I would like the output to be this sequence printed 3 times using the InitSeed.

Code: Select all

CLS
RANDOMIZE TIMER
DIM InitSeed AS DOUBLE: InitSeed = -RND
PRINT "Three times, you say? OK:"
y = RND(InitSeed)
FOR i = 1 TO 10: PRINT INT(100 * RND); : NEXT i: PRINT
y = RND(InitSeed)
FOR i = 1 TO 10: PRINT INT(100 * RND); : NEXT i: PRINT
y = RND(InitSeed)
FOR i = 1 TO 10: PRINT INT(100 * RND); : NEXT i: PRINT
PRINT : PRINT "Once without InitSeed, once with it:"
RANDOMIZE TIMER
FOR i = 1 TO 10: PRINT INT(100 * RND); : NEXT i: PRINT
y = RND(InitSeed)
FOR i = 1 TO 10: PRINT INT(100 * RND); : NEXT i: PRINT
PRINT : LINE INPUT "Press Enter"; e$
CLS : SYSTEM
Got it?

Mac

Repeating Random Numbers

Posted: Thu Mar 20, 2008 3:12 pm
by DDastardly71
Works perfectly. Exactly what I needed.

THANK YOU