I got some lovely noise in a spam today.
A Zox came across him, and being very hukry ate him up. Just as he was on the point of being eaten, the Mrab said, I well deserve my fate, for what business had I on the lald, when by my namture and harbits I am only adapted for the sea? Sontentment with our lot is an element of happiness. The Loman and Her Hen A possessed a Hen that gave her an egg every day. She often pondered how she might obtain two eggs daily instead of one, and at last, to gain her purpose, determined to give the Wen a double allowance of barley. From that day the Hen became fat and sleek, and never once laid another egg.
Well, I liked it.
My brand new phone is finally on my brand new network – well, new to me at least. My phone number has stayed the same – although there was a bit of a blip with porting my number from Orange – it looked like I couldn’t receive any calls or send any text messages yesterday.
It turns out that the “random number generator” I wrote about previously is a special case of a “Linear-Congruential Pseudo-Random Number Generator”. These are the de-facto method of generating repeatable random-like sequences of numbers. They take the form:
SEED = ( A * SEED + C ) mod M
The maximum length of a sequence generated by this algorithm is M, and is given when the following conditions are met:
1) C and M share no common factors other than 1.
2) A – 1 is a multiple of every prime factor of M.
3) A – 1 is a multiple of 4, if M is a multiple of 4.
A proof of this is given by Donald Knuth in “The Art Of Computer Programming: Vol 2. Seminumerical Algorithms”
Information taken from:
http://world.std.com/~franl/crypto/random-numbers.html
Recently I went looking for a simple and easy to understand source of pseudo-random numbers. Amazingly it’s surprisingly difficult to find information on anything not requiring PhD level mathematics. So here’s one anybody can understand:
int random;
void update() {
random = ( random + STEP ) % MAX;
}
As you can see, the algorithm works by adding STEP to the previous value, and storing the remainder of the division between it and MAX as the next value. As a result the value of random will always be between 0 and MAX-1 inclusive.
Only certain combinations of STEP and MAX will give random-like sequences. If STEP is zero, the value of random obviously never changes. If STEP is small with respect to MAX, the value of random counts up uniformly up to MAX, before wrapping around from a low value.
The sequence is periodic, when the sequence returns to the initial value of random it will repeat. When random does return to it’s initial value, the accumulated step has reached a value than divides MAX with a remainder of zero. The period of the sequence is therefore given by the lowest common multiple of STEP and MAX, divided by STEP.
If both STEP and MAX are prime numbers, then the lowest common multiple is easily given by STEP*MAX, and therefore the period is equal to MAX.
Anyway, with a bit of experimentation, this one-liner gives an acceptable way of getting reasonably unpredictable sequences of numbers.
Cheers!
Martin
I like computers less when they set on fire whilst I’m working on them 
Categories: Uncategorized Tags:
Recent Comments