Hi Cenk,
thanks for bringing this up!
Can you add the POSIX perspective to the rand* calls? I guess it would be beneficial to remain close to what POSIX describes for an API.
Cheers, Thomas
Hi Cenk,
thanks for bringing this up!
Can you add the POSIX perspective to the rand* calls? I guess it would be beneficial to remain close to what POSIX describes for an API.
Cheers, Thomas
Hey,
oh sure, here it follows:
The ISO C standard rand() and srand() functions allow per-process
pseudo-random streams shared by all threads. Those two functions need
not change, but there has to be mutual-exclusion that prevents
interference between two threads concurrently accessing the random
number generator.
With regard to rand(), there are two different behaviors that may be
wanted in a multi-threaded program:
1. A single per-process sequence of pseudo-random numbers that is shared
by all threads that call rand()
2. A different sequence of pseudo-random numbers for each thread that
calls rand()
This is provided by the modified thread-safe function based on whether
the seed value is global to the entire process or local to each thread.
See: http://pubs.opengroup.org/onlinepubs/9699919799/functions/rand.html
The above prose makes me wonder: our current global-state-PRNG-implementations do AFAIK not provide mutual exclusion.. So, two threads accessing the PRNG could get in each other's way (?). And this is not just about `random_init()`, but all calls that modify an existing PRNG sequence, such as a simple `rand()`.
Just as info: in the above link it says:
The drand48() and random() functions provide much more elaborate
pseudo-random number generators.
and those do actually allow to intuitively set custom states, or do mutual exclusion: http://pubs.opengroup.org/onlinepubs/9699919799/functions/random.html http://pubs.opengroup.org/onlinepubs/9699919799/functions/drand48.html
Cheers, Cenk