!pr2
More Random Number Generators..............Bob Sander-Cederlof

I published my "Random Numbers for Applesoft" article last month just in time.  The June issue of Micro includes a 9.5 page article called "A Better Random Number Generator", by H. Cem Kaner and John R. Vokey.  The article reports on work funded by the Natural Sciences and Engineering Research Council of Canada (NSERC).

The authors give an excellent overview of the various methods used to test random number generators, and the methods they used during their seven years of research to produce three "better" generators.  It is worth buying a copy of Micro to get a copy of this article.

The authors used the same linear congruential algorithm I discussed last month, but with different parameters.  My favorite last month was:

       R(new) = ( R(old) * A + C ) mod 2^32
       where A = 314159269
         and C = 907633386

Kaner and Vokey decided to use a 40-bit seed and use mod 2^40 in calculating each successive value.  After extensive analysis and testing, they came up with three generators based on the following values for "A" and "C":

       RNG 1:  A = 31415938565
               C = 26407

       RNG 2:  A = 8413453205
               C = 99991

       RNG 3:  A = 27182819621
               C = 3

There are an unusually large number of typos in the article, and some of them are hard to decipher.  The value 26407 above was written in the comment field as 24607; however, in the hexadecimal constant assembly code it was 0000006727, which is 26407.  Even worse, in the listing there are missing lines of code and missing characters here and there.  All of the immediate mode instructions are missing a "#" character.  Four or five labels are never defined in the listing.

Since the program as printed cannot possibly be successfully loaded, assembled, POKEd, or executed, I have chosen to re-write it for inclusion here, after my own style.  I believe my version is also significantly improved in coding and speed.

The reason given for choosing to work with 40 bits rather than 32, even though Applesoft only stores 32 and using 40 takes longer, was that it is important to provide more values between 0.0 and 2^-32.  I tend to disagree on the importance of this, since most uses of random numbers on the Apple are for games, and simulate such simple things as dealing cards or throwing dice.  Perhaps more serious simulations need more precise randomness.  Of course they also increase by a factor of 256 the number of numbers generated before the sequence repeats.

Buried in the middle of the program is a well-optimized 40-bit multiplication loop.  You might enjoy puzzling out how it works!

The program uses USR(x), where x selects which of the three generators to use.  There is no provision for setting the seed or for selecting a range other than 0...1, such as I included in my programs last month.  Some enterprising individual will marry the shell of my USR subroutine with the RNG of Kaner and Vokey to produce a really great Applesoft Random Number Generator.
