The way I learned was to initially seed the random number generator with srand(time(NULL))
and then use calls to rand()
to generate random numbers. The problem with this approach is if I run my program multiple times in the same second, the random numbers generated will always be the same. What is a good way around this?

- 21,026
- 18
- 63
- 115
-
You could resort to a system-specific method for more precision. For example, Windows has `GetTickCount`. *nix has [clock_gettime](http://linux.die.net/man/3/clock_gettime). – chris Apr 06 '13 at 02:43
-
Your title and question body are asking two different things. – Pubby Apr 06 '13 at 02:44
-
Don't run the program in quick succession? Get a timer with better precision? – Jeff Mercado Apr 06 '13 at 02:44
-
2Well, you can mix many sources of information to use as random seed. `stime(time(NULL) * getpid())` for instance, that would do the trick. – Havenard Apr 06 '13 at 02:51
-
@Havenard Is multiplication the best way to combine them? Would xor work too? – Matt Apr 06 '13 at 03:01
-
1XOR would be rather bad because pid typically increases by 1 on each run, and if time also increased by 1, you could easily get the same result. – R.. GitHub STOP HELPING ICE Apr 06 '13 at 03:45
-
I guess doesn't really matter. – Havenard Apr 06 '13 at 03:59
5 Answers
On POSIX systems, use clock_gettime
to get the current time in nanoseconds. If you don't need a lot of bits, you can just forget the PRNG and use the low-order bits of the time as your random number directly. :-)

- 208,859
- 35
- 376
- 711
-
I'm guessing somebody didn't like my idea of using the low bits of the current time for RNG. However, given the factors that affect them (memory latency, cache hits/misses, TLB misses, swapping, interrupt timing, scheduling, ...) and the fact that nanoseconds correspond roughly to cycles on modern machines, they're a pretty damn good entropy source. – R.. GitHub STOP HELPING ICE Apr 11 '13 at 23:30
-
I concur. It is both clever and elegant. Hence my question. I looked puzzled at the downvote for such appropriate answer to issue at hand. It pretty much solves the question (running the program multiple times in the same second and getting the same result each time). – Jean Apr 11 '13 at 23:36
int pid ; // get it as per your OS
timeval t;
gettimeofday(&t, NULL);
srand(t.tv_usec * t.tv_sec * pid);
time gives you values based on second
. gettimeofday is based on microseconds
. So less chance of the same seed happening. Plus you are also using the process id.

- 13,733
- 8
- 60
- 122
If *nix, Why don't you read directly from /dev/random
?
Also you can gather noise from other devices, like the keyboard, mouse or the CPU temperature.
You can use an accelerometer and use it to gather noise from sea waves. The Wind also produce noise.
I believe Glib provides a function, g_random_int()
which produces random numbers equally distributed in a fast and portable way.
Or you can just read the numbers of temporal files in /tmp
and use that number to feed srand()
with a time.h
function, or read the content of one file in /tmp
.
You can read each file from /usr/bin
or /
and gather some food for srand()
.

- 2,954
- 2
- 29
- 40
Besides using time, another common way to seed your rand function is to use the process id of your program, since that is guaranteed to be unique.
The actual code is platform-dependent, but if you're on Windows, I believe you can use the function GetCurrentProcessId()
, as in
srand(GetCurrentProcessId());

- 719
- 1
- 10
- 23
-
2In this way, You will get the same rand number sequence everytime you call the `rand` function. – prehistoricpenguin Apr 06 '13 at 02:53
-
-
2@prehistoricpenguin, Only if your process has the same PID every time. That's why it's a good idea to mix it with a time function. – chris Apr 06 '13 at 02:56
Beside inputting the time, you could add the CPU time to this, which I believe can be do with clock().
So it would look like this: srand(time() + clock())
.

- 216
- 1
- 12