MATH_RND Algorithm

The MATH random number routines work on random number state information. There is a current set of state info defined globally which defines 32 bytes of state data which provides a very good generator compared to normal linear congruential generators. The MATH_IRND and MATH_RND routines have no arguments and simply get the next number in the sequence from the current state info. MATH_SETRND( seed ) lets the seed of the current generator be set. MATH_INITRNDSTATE( seed, statedata, nbytes ) sets the current state data area to which must be at least long. The generator is initialised with . This routine returns the address of the old state data as an integer value. The MATH_SETRNDSTATE( statedata ) enables state info to be swapped. The old state data address is returned by this function.

An improved random number generation package. In addition to the standard rand()/srand() like interface, this package also has a special state info interface. The MATH_INITRNDSTATE() routine is called with a seed, an array of bytes, and a count of how many bytes are being passed in; this array is then initialized to contain information for random number generation with that much state information. Good sizes for the amount of state information are 32, 64, 128, and 256 bytes. The state can be switched by calling the MATH_SETRNDSTATE() function with the same array as was initiallized with MATH_INITRNDSTATE(). By default, the package runs with 128 bytes of state information and generates far better random numbers than a linear congruential generator. If the amount of state information is less than 32 bytes, a simple linear congruential R.N.G. is used. Internally, the state information is treated as an array of longs; the zeroeth element of the array is the type of R.N.G. being used (small integer); the remainder of the array is the state information for the R.N.G. Thus, 32 bytes of state information will give 7 longs worth of state information, which will allow a degree seven polynomial. (Note: The zeroeth word of state information also has some other information stored in it; see MATH_SETRNDSTATE for details). The random number generation technique is a linear feedback shift register approach, employing trinomials (since there are fewer terms to sum up that way). In this approach, the least significant bit of all the numbers in the state table will act as a linear feedback shift register, and will have period 2^deg - 1 (where deg is the degree of the polynomial being used, assuming that the polynomial is irreducible and primitive). The higher order bits will have longer periods, since their values are also influenced by pseudo-random carries out of the lower bits. The total period of the generator is approximately deg*(2**deg - 1); thus doubling the amount of state information has a vast influence on the period of the generator. Note: The deg*(2**deg - 1) is an approximation only good for large deg, when the period of the shift register is the dominant factor. With deg equal to seven, the period is actually much longer than the 7*(2**7 - 1) predicted by this formula.