J. C. Leitão - 5 months ago 47

C Question

I'm currently working on a C/C++ project where I'm using a random number generator (gsl or boost). The whole idea can be simplified to a non-trivial stochastic process which receives a seed and returns results. I'm computing averages over different realisations of the process.

So, the seed is important: the processes must be with different seeds or it will bias the averages.

So far, I'm using

`time(NULL)`

So, my question is: how to implement a "seed giver" on C/C++ which gives independent seeds?

For instance, I though in using the thread number (

`thread_num`

`seed = time(NULL)*thread_num`

The requirements are that it must work on both Mac OS (my pc) and Linux distribution similar to OS Cent (the cluster) (and naturally give independent realisations).

Answer

We ran into a similar problem on a Beowulf computing grid, the solution we used was to incorporate the pid of the process into the RNG seed, like so:

```
time(NULL)*thread_num*getpid()
```

Of course, you could just read from /dev/urandom or /dev/random into an integer.