Qbasicnews.com

Full Version: Re-seeding issue
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
While planning to develop a game in QB, how do we re-seed the random number generator if a player wishes to play again with the same sequence? The code below reveals the second sequence is differed from the first.
Code:
DIM seed AS LONG

CLS
seed = TIMER

RANDOMIZE seed
FOR i = 1 TO 3
  PRINT INT(RND * 9999) + 1
NEXT
PRINT

'Re-seed
RANDOMIZE seed
FOR i = 1 TO 3
  PRINT INT(RND * 9999) + 1
NEXT
Is this a Microsoft way to generate a different sequence in spite of trying to re-seed the RNG?

I know several solutions such as writing your own RNG or putting sequence in an array. It is not a good practice, tho'.
To seed RND, just set some bogus variable to RND(-seed)

Mac

Code:
CLS
DO
  RANDOMIZE TIMER
  seed = RND * 999999
  y = RND(-seed)
  FOR i = 1 TO 5: PRINT RND; : NEXT i: PRINT
  y = RND(-seed)
  FOR i = 1 TO 5: PRINT RND; : NEXT i: PRINT
  INPUT "Again"; a$
LOOP WHILE LEFT$(UCASE$(a$ + " "), 1) = "Y"
CLS
SYSTEM
Thanks, Mac!

Your code works as expected. The help file does not show a good example for re-seeding technique.

BTW, your code is very clean and a good style.

Spoken by an old pro.

Geo
Quote:Spoken by an old pro.
How do you know he is an old pro? :normal:
It is me! Not him.

Known many languages from my long experience. QB is just one of them. I just need refreshing on QB that I had known a long time ago.

So glad you guys are still around here. Smile