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.
Repeating Random Numbers
Moderators:Administrator, Global Moderator
Re: Repeating Random Numbers
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
Mac
- DDastardly71
- Jr. Member
- Posts:15
- Joined:Sun Jan 20, 2008 11:25 pm
Repeating Random Numbers
Works perfectly. Exactly what I needed.
THANK YOU
THANK YOU