Repeating Random Numbers

Please use this Board for QBasic related requests ( file research, programming, etc.)

Moderators:Administrator, Global Moderator

Post Reply
User avatar
DDastardly71
Jr. Member
Posts:15
Joined:Sun Jan 20, 2008 11:25 pm
Repeating Random Numbers

Post by DDastardly71 » Wed Mar 19, 2008 6:31 pm

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.

Mac
Full Member
Posts:48
Joined:Wed Jun 29, 2005 2:01 am
Contact:

Re: Repeating Random Numbers

Post by Mac » Thu Mar 20, 2008 8:56 am

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

User avatar
DDastardly71
Jr. Member
Posts:15
Joined:Sun Jan 20, 2008 11:25 pm

Repeating Random Numbers

Post by DDastardly71 » Thu Mar 20, 2008 3:12 pm

Works perfectly. Exactly what I needed.

THANK YOU

Post Reply