http://sourceware.org/git/glibc.git。 由此可见,执行工作在随机生成的锁下,因此,在温热道路的多面环境中利用这种执行是不正确的。
/* If we are using the trivial TYPE_0 R.N.G., just do the old linear
congruential bit. Otherwise, we do our fancy trinomial stuff, which is the
same in all the other cases due to all the global variables that have been
set up. The basic operation is to add the number at the rear pointer into
the one at the front pointer. Then both pointers are advanced to the next
location cyclically in the table. The value returned is the sum generated,
reduced to 31 bits by throwing away the "least random" low bit.
Note: The code takes advantage of the fact that both the front and
rear pointers can t wrap on the same call by not testing the rear
pointer if the front one has wrapped. Returns a 31-bit random number. */
long int
__random (void)
{
int32_t retval;
__libc_lock_lock (lock);
(void) __random_r (&unsafe_state, &retval);
__libc_lock_unlock (lock);
return retval;
}
weak_alias (__random, random)
Instead, you can create thread_local
instances of generators to mitigate the bottleneck:
C++:
thread_local std::mt19937 generator(42 /* seed (can be e.g. time)*/);
void thread_safe_rand(int n) {
std::uniform_int_distribution<int> distribution(0, n-1);
return distribution();
}