Before we get to the fun parts, though, a bit of serious commentary. The rand and random functions are specified by C and POSIX, respectively, to have initial seeds of 1 and to be reseedable via srand and srandom. That has been interpreted to mean that the same seed must always produce the same sequence, but the actual guarantee is quite a bit weaker than that. The C standard says very little about the rand algorithm, and POSIX doesn’t add much except random must be “a non-linear additive feedback random-number generator”. The key point, however, is that there are many such generators, all of which produce different sequences even with identical seeds. Different operating systems are not guaranteed to produce the same sequence. In fact, repeated executions of the same program are not guaranteed to produce the same sequence. Each execution could pick different internal multipliers. Or a fully conforming implementation may well contain seed ^= getpid() at the top of srandom. Repeatable within the program, but not afterwards. The same sequence language only applies within the execution of a single process. Any program which depends on generating the same sequence with the same seed after multiple executions is in fact depending on undocumented, nonstandard, implementation specific behavior. Here’s a nickel kid; get a better language lawyer.
But who cares about standards? Let’s look at some code.
A popular choice for a seed is 0. This is not so very different from the default of 1, but some people want their numbers to be a little different. But not too different.
Considering these two lines from the srandom implementation, not too different at all.
(Now may be a good time to point out that the relevant standards do not specify that different seeds produce different sequences.)
Some people have read a book.
Using your birthday is another choice.
Younger programmers prefer srand48.
Bigger numbers are also common.
Careful, that’s a 6 not a 5. Unpredictable, no?
Hexadecimal is the preferred numeric notation of the serious programmer.
Humor is common.
When a fixed seed doesn’t work, maybe the current time will.
Old timers are old school.
If seconds aren’t precise enough, try microseconds.
Or seconds and microseconds, all together.
If performance is important, cache the time.
Maybe you want your time to be different from other people’s time.
For extra variation, try adding in the pid.
Or literally adding in the pid.
Add literally oring in the pid.
All together now.
Perhaps multiplication with a side of completely unnecessary modulus?
Here’s one with a completely necessary modulus. We wouldn’t want our random numbers to get too random.
The one operation that was not observed was substracting the pid from the time. More research into this subject is warranted.
Another approach one can take is self seeding. It’s the same, but different.
Future proof your code by allowing the seed values to increase over time.
A little of this, a little of that.
Much more sophisticated seeding methods are also possible.
Remember to always initialize the seed from a copy of the version string, not the original.
If you don’t have a version string, pretend you do.
Don’t be afraid to reseed too frequently. Once per random number is good if your program runs slowly.
The multitude of random number generators also permits cross seeding.
If you need a really good seed, look to the OpenSSL RAND functions.
Take 16 bytes of random data. No, wait, make that 15 bytes. Then hash it to four bytes to really squeeze the entropy in. Then seed.
Of course, this can be done both ways. The output of rand can be used to seed OpenSSL RAND. Or attempted, anyway.
Yes, you are reading that correctly. Seed rand with the current time, then print a random number into a buffer. Convert the buffer back into a number. Discard the number. Seed RAND with a different, uninitialized array. I don’t use this program. Maybe you do.
The deficiencies of rand have been known for some time, leading some software to work around them.
One can also try varying the output a bit.
Apart from the fact that signed integer overflow is undefined behavior, that’s a great way to turn a uniform random number generator into a biased random number generator.
I ran out of funny trying to describe what’s happening here.
Another amusing code snippet observed.
At least this way the code looks like it’s using unpredictable numbers. Another form of deception is to leave misleading comments.
Much better without the comments.
Now, of course, maybe all these examples are quite safe in context and I’m over reacting because nobody in real life would ever use rand to generate disk encryption keys.
TIFU by using Math.random has some interesting details about how just how bad random can be.
I guess it’s good news that malware authors repeatedly make the same mistakes?
Voting machines make it easy to guess the seed time by printing it out for you.
It was noted in 2012 that libexpat random is weak. And then nothing happened for three years. Then came a 2015 redhat bug. Now in 2016 it’s not one but two more CVEs. One could use arc4random like OpenBSD, but then your secret hashes would be unpredictable.