## Seeding Random Numbers

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Zim
Veteran
Posts: 98
Joined: Mon Dec 05, 2005 4:31 pm
Location: Wisconsin, USA
Contact:

### Seeding Random Numbers

Ok, my turn...

There are a couple of topics here dealing with random number/letter generation. My question deals with starting, or seeding Basic's random number genereator (rng).

Generally that is done with the Randomize statement using "timer" as the seed value:

Code: Select all

RANDOMIZE timer
But I have a need to re-seed the rng at a given point in its sequence so that I may reproduce the sequence at a later point in time.

I use:

Code: Select all

dummy=rnd(-seed)
because giving a negative value to the rnd() function as an argument re-starts the sequence at a given point.

Now, back to "Randomize". If you execute that statement with no argument, Q/QuickBASIC will prompt for a seed in the range of -32768 to 32767, the range of two-byte integers. However, you can respond with any 4- or 8- byte real and it appears that it works. However, the sequence is still started randomly.

If I try the same thing with "dummy=rnd(negative value)" it also appears to work AND I re-start at a known point.

Now my question: How many bytes are ACTUALLY used to restart the sequence? Are there really only 65,535 different starting points? Or, since I can use larger values are there more starting points? If I use "timer" I have about 86400 seconds per day times 18.2 timer ticks per second, or about 1.5 million different seed values. In otherwords, how many bytes make up the internal state of BASIC's rng? It's probably more than two, but is it three?, four?, eight? (Not likely.)

I'd be interested to know how many different (potential) sequence seeds I really have. Any bites?
--- Zim ---
--- Time flies like an arrow, but fruit flies like a banana ---

Michael Calkins
Veteran
Posts: 76
Joined: Tue Apr 05, 2005 8:40 pm
Location: Floresville, Texas
Contact:
read this thread:

http://www.network54.com/Forum/178565/m ... andom+post.

thanks to stylez, zombie, etc. for that.

Regards,
Michael
Bring on the Maulotaurs! oops...
I like to slay Disciples of D'Sparil...

Guest
there is an article in the quickbasics knowledge base of Microsoft talking about the RND and Randomize. in that article are shown two examples using the type of formula used by the quickbasics to generate the pseudo-random numbers. it can be read that only are used double-precision numbers. so i can think that to begin a sequence of numbers you can use any double-precision number as a seed.
you can read that article Right Here.
Zombie found the real values used by Microsoft in the quickbasics that could be found in the old Qbasic.com. he did it working the formula in the toughest way.

Macric
Coder
Posts: 34
Joined: Fri Mar 25, 2005 11:11 pm
Location: Mexico
whoops, the previous post it's mine, i forgot to log in first. bye.

Zim
Veteran
Posts: 98
Joined: Mon Dec 05, 2005 4:31 pm
Location: Wisconsin, USA
Contact:

### How rnd really works

Thanks guys! That's exactly what I was looking for. I have suspected for a long time that my (qb 3.0) rnd state is only 3 bytes long. This confirms it. I'll take the code and try it in both qb 3 and qb 4. (BTW, the rnd output in GW-BASIC is different. I suspect it has slightly different parameters.)
--- Zim ---
--- Time flies like an arrow, but fruit flies like a banana ---