Page 1 of 1

Seeding Random Numbers

Posted: Fri Dec 16, 2005 12:17 pm
by Zim
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?

Posted: Fri Dec 16, 2005 10:08 pm
by Michael Calkins
read this thread:

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

thanks to stylez, zombie, etc. for that.

Regards,
Michael

Posted: Sat Dec 17, 2005 7:19 am
by 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.

Posted: Sat Dec 17, 2005 7:25 am
by Macric
whoops, the previous post it's mine, i forgot to log in first. bye.

How rnd really works

Posted: Mon Dec 19, 2005 12:50 pm
by Zim
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.)