[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Where does the Applesoft RND() function get its data?
David Empson wrote:
vladitx <vladitx@nucleusys.com> wrote:
On Feb 25, 10:48 am, "Jayson Smith"
<ihatespamratguynospample...@insightbb.spamsucks.com> wrote:
I'm just wondering where the Applesoft RND() function gets its numbers. Is
there a spot in memory that contains random values, or a machine language
routine, or what?
Just by memory - I think the subroutine that produces pseudo-random
numbers uses entropy from a counter incremented by the KEYIN loop.
Nope. There is an entropy generator ($4E and $4F are incremented as a
16-bit number while waiting for a keypress) but there is nothing to
connect that automatically to the Applesoft RND() function.
By itself, RND() is a standard pseudo-random number generator which uses
a formula to calculate the next number in a sequence, which will
eventually wrap around and repeat the same pattern. I don't think I ever
knew the specific algorithm used by Applesoft.
It's a modified linear congruential generator. The seed (or previous
generated number) is multiplied by a constant, then a constant is added
and (the modification) the high and low bytes of the mantissa are
exchanged.
Unfortunately, the choice of constants is bad, resulting in a period
*much* shorter than 2^32-1. Further, there is a bug in Applesoft
initialization which causes the low byte of the initial seed to not
be initialized, so the default sequence is not repeatable unless the
function is explicitly seeded.
The "wait for keypress" method is a good way to _seed_ the random number
generator. My vague recollection is that you seed RND() by passing it a
negative number, which you could construct from values PEEKed from
$4E/$4F.
If you want different sequences of random numbers on each run of a
program, this method of seeding is good. If you want repeatable
sequences, seeding with a constant is necessary.
-michael
NadaNet and AppleCrate II: parallel computing for Apple II computers!
Home page: http://home.comcast.net/~mjmahon
"The wastebasket is our most important design
tool--and it's seriously underused."