I was bored last night and decided to teach myself something. I had always wondered how a logical machine like a computer can generate a truly random number, so I done a little research.
Just thought I'd share my findings on the off chance that I wasn't the only person in the world who was wondering!
In basic - there are two types of computer generated random numbers:
You could set a device to monitor a random natural event - a common example tends to be to place a piece of radioactive material in front of a Geiger counter with Geiger counter connected to a computer.
Since radioactive decay is random, the Geiger counter could create truly random numbers.
This approach is pretty rare, for the obvious (I hope) reason that many people do not have Geiger counters connected to their lovely machines.
You can do the much more common approach - and generate an almost
random number, as per the case with most computers.
In a simple terms a formula that generates what is known as a 'pseudo-random number'
is created. The idea of the formula is for it to produce a string of numbers that would look random to anyone who did not know the formula itself.
An example of a simple random-number formula from the book "The C Programming Language," by Kernighan and Ritchie:
random_seed = random_seed * 1103515245 +12345;
return (unsigned int)(random_seed / 65536) % 32768;
This formula assumes the existence of a variable (start number) called random_seed, which is initially set to some number. The random_seed variable is multiplied by 1,103,515,245 and then 12,345 gets added to the product and some more lovely mathmatical dividing and adding up. You then get out your first almost
random value. Next time the script is run random_seed is then replaced by this new value creating an even more random value.
However can you see the problem here? You need a default value to start the formula. All you've done so far is change a few numbers around. Nothing random about it. If you always used the same value at the start you would always return the same numbers in the same order. So you need a random number to start of the random number sequence?? This was the bit that always used to stump me.
The simple answer is.....
It's impossible to create a truly random number for this value (unless you use the first method listed above) - what most machines resort to is second best - they convert the current date and time into an integer (for example the number of seconds or even miliseconds since 1st Jan 1970) and use this as the magic starting value for the seed.
They then have an always changing number to start off the formula, and hence always generate a different set of values each time the forumla is run!
So there you have it, computers and almost random
, random numbers.