I took a hard look at the RNG you used. Here is what I see.
The formula you use is in general OK. However in theoretical mathematics the %2147483647 plays a key role; for numbers are otherwise unbounded in a mathematicians world. If an implementation is done in 64bits or more it would be likewise. As implemented, using 32 bit registers, the numbers generated are already clipped nearly the same (all be it not quite as desirably) as would the %2147483647. Performing this modulo in addition, to the one built into the 32 operations, is effectively worthless and a waist of CPU cycles.
Upon testing the randomness of number sequences generated, it is seen that removing the modulo operation makes no difference. I have verified, (with “ENT” from www.fourmilab.ch/random ) that generated number sequences yield good figures for all the tests (Entropy, Chi-square, Mean, Monte-Carlo-Pi, Serial-Correlation) except for very poor performance on the Chi-square test, which is perhaps the most important and best predictor of generating random numbers. Examining outputs I found that the low order 8 bits after an initial couple of hundred numbers begins cycling a series of hundred or so in length.
This can be fixed like this:
u32 g_seed;
. . .
u16 Square_rand (void)
{
g_seed = ( 1103515245 * g_seed + 12345 );
return (g_seed>>16);
}
This yields 16 good bits of randomness, any part of which can be used to produce different ranges of values. All of the ENT test results are quite good. By the way, with yet another trick a good random set of 32bits could be generated and without using 64 bit math.
So try it out. I Hope you make some more games for our society.
Ron Miller (zero-8.com)
Last edited by rondesc (2007-10-11 18:14:37)