Just a little random
I was helping a friend out over the weekend with a project and we got to the access control unit where he wanted to have long sequences of random numbers for the access key. "Fine," I said, "lets go find some random numbers." Sure you could use rand(), but unix random numbers have never had much respect. Long ago I pulled up a pseudo-random routine from the back of some programming magazine (that's how we learned about new stuff before the internet), and that's sat around in my toolkit for quite some time:
#define Ra 1629
#define Rm 1048576
static unsigned long ran_seed ;
void init_rand() { ran_seed= time() % Rm ; }
int get_rand(int n)
{
ran_seed= (long) (Ra * ran_seed) % Rm ;
return (n * ran_seed) / Rm ;
}
This is fine and all when you just need a few random bits for a game of hangman, or picking a random
fortune, but its not so good when having something be random actually is important. There are of course larger pseudo-random cycles, and ways to
whiten the data to avoid bias, but you're still dealing with something that's predictable on some scale or another.
At some point, once cyptrography became more widespread, having something better became more important and people started looking for real randomness. One such attempt was /dev/random which counted all the generic processes going on inside a computer, accumulating them together to produce random results. The length of a disk seek, the number of times an event was triggered, the checksum bits from various network packets received; thrown together and shaken. This produced decent results when used sparingly, but still not really random. If you're willing to spend enough money, you can buy these really cool boxes that have a radioactive isotope or high temperature thermal emitter and a detector that counts the picosecond spaces between individual particle emissions, but for something casual, these might be overkill.
Still, this is 2006, not 1984; there ought to be some solution out there perfect for the casual user:
Ah, web 2.0 to the rescue: random.org, using background radio noise from the atmosphere as an entropy source, this site produces numbers, sequences, raw bytes or even pictures.
HTML, http, soap and Corba are all welcome.