Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random number generators and thread safety

Tags:

c++

c

random

Almost every pseudorandom generator in C/C++ (Mersenne, ...) uses some kind of internal state, usually a short vector of bytes. My question is, when such a random generator is used and shared in a multithreaded environment is it "much" better to have it thread-safe or letting "race conditions" to occur can only increases randomness?

I know this question is extremely hard to answer rigorously but will appreciate any opinions.

like image 338
Cartesius00 Avatar asked Nov 30 '25 05:11

Cartesius00


1 Answers

Letting "race conditions" occur can mess up everything. Technically, a data race is undefined behaviour, so it could order pizza.

But even if that doesn't happen, the internal state is likely to get corrupted and all important properties of the random sequence will just be lost. You can no longer guarantee uniformity, for example. You can't leave the generation of random numbers to chance.

like image 91
R. Martinho Fernandes Avatar answered Dec 02 '25 21:12

R. Martinho Fernandes