What is the best way to seed srand()? [duplicate]
Asked Answered
F

5

10

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?

Framing answered 6/4, 2013 at 2:42 Comment(7)
You could resort to a system-specific method for more precision. For example, Windows has GetTickCount. *nix has clock_gettime.Dialectology
Your title and question body are asking two different things.Primordial
Don't run the program in quick succession? Get a timer with better precision?Moderate
Well, you can mix many sources of information to use as random seed. stime(time(NULL) * getpid()) for instance, that would do the trick.Putto
@Putto Is multiplication the best way to combine them? Would xor work too?Framing
XOR 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.Oeflein
I guess doesn't really matter.Putto
O
6

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. :-)

Oeflein answered 6/4, 2013 at 3:46 Comment(2)
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.Oeflein
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).Crashing
K
2

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().

Kizzie answered 6/4, 2013 at 5:0 Comment(1)
Mr -1, I'm looking forward to read the reasons.Kizzie
A
1

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());
Arizona answered 6/4, 2013 at 2:51 Comment(3)
In this way, You will get the same rand number sequence everytime you call the rand function.Normand
@Normand What do you mean?Framing
@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.Dialectology
D
1
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.

Dimmick answered 6/4, 2013 at 2:59 Comment(1)
t.tv_usec * t.tv_sec * pid is a weak choice. That product is converted to an unsigned when given to srand(unsigned). Each power-of-2 value of the 3, reduces the bits available for the seed. Instead use ^: t.tv_usec ^ t.tv_sec ^ pid.Earth
L
-2

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()).

Lille answered 6/4, 2013 at 2:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.