Applesoft: Random numbers

All numbers produced by running Applesoft's random number generator follow the same sequence as the first RUN, since the Applesoft's random number seed is not reinitialized.

This seed number, stored in locations $C9 to $CD, is initialized when your system is first booted.
The Applesoft random number generator is only a pseudo-random generator (as are most generators); thus, non-random patterns eventually occur. The frequency these patterns repeat varies from program to program, though proper re-seeding of the random number generator helps prevent small repeating sequences.

Here are two suggestions:

1. Use the monitor's random seed at locations 78 and 79 to initialize Applesoft's random number seed. Since the monitor's seed is constantly incrementing while waiting for a key to be pressed, you'll start on one of 65536 different sequences.

10 X = RND (-PEEK (78) - PEEK (79) * 256)

2. This random sequence can be lengthened by re-seeding the generator occasionally within the application program by adding the statement X = RND (-RND (1)).

No method completely eliminates patterns in the random numbers generated, but you can lengthen the sequences until they're difficult to detect.

Applesoft's algorithm is:
The example below shows the effect of the algorithm on a number. The most and least significant two decimal digits are swapped instead of bytes in this example:

0.500000000
* 11879546.4
-----------
5929773.2
+ 0.0000000392767778
-------------------------
5929773.2 (the rest is lost)
-- --- swap "bytes"
3229775.9

0.32297759 force value between 0 and 1
Published Date: Feb 20, 2012