Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Re-seeding issue
#1
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'.
Reply
#2
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
Reply
#3
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
Reply
#4
Quote:Spoken by an old pro.
How do you know he is an old pro? :normal:
Reply
#5
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
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)