There has been much talk of random numbers here recently, both real and pseudo. What with Cessnapilot's suggestion to use CNT as a random source at start up and Bit's (was it?) request for a random shuffling algorithm.
This reignited my long standing fascination for such things and the result is my Christmas break project was one super-duper random number generator for the Propeller which is attached here.
The simple idea is:
1) We would quite often like to use genuine random numbers form some random source. At least so that our programs don't behave the same way on every run as they might with Spins random operator. An easy way to do that is with Chip's magical RealRandom object. BUT that consumes a whole valuable COG for such a simple task.
2) So we could start up RealRandom, get real random seed from it, shut it down to save a COG and then use the seed for Spins built in random operator in our applications.
3) BUT whilst we are at it why not use a pseudo random number generator with a huge repetition period and probably more random looking sequences? That way we would be hard pressed to tell the difference between using RealRandom all the time or a randomly seeded PRNG.
Most high quality PRNGs use unsigned arithmetic and/or 64 bit arithmetic and/or multiplies and are therefore not very Spin friendly. Luckly over Christmas I found the JKISS32 algorithm by David Jones, based on ideas by the late George Marsaglia, an expert in PRNG's. JKISS32 uses 32 bit signed arithmetic, only uses a handful of lines of Spin and has a period of ~2^121. That is the "random" number sequence it generates will only start to repeat after about 2600000000000000000000000000000000000 numbers !!! (if I have counted my zeros correctly). JKISS32 has been tested with some serious statistical tools to check the quality of it's "randomness" and passed with flying colours.
P.S. Again I have to say I don't imply that Spin's random operator is faulty but sometimes we might like to have better. For example Bit's shuffling project could do with very long period PRNG. Also it can be beneficial to not use built in system RNG's and use you own, this way when you move your code from system to system you can be sure of consistent high quality results.