2022-04-19 19:23:30 -04:00
|
|
|
static QMutex l_rndMutex; // mutex to protect the random number generator
|
2012-08-14 18:00:48 -04:00
|
|
|
|
2015-12-31 14:56:37 -05:00
|
|
|
void BSP::randomSeed(uint32_t seed) {
|
2022-04-19 19:23:30 -04:00
|
|
|
l_rndMutex.init(N_PHILO + 1U); // <--- initialize the mutex
|
2015-12-31 14:56:37 -05:00
|
|
|
l_rnd = seed;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t BSP::random(void) { // a very cheap pseudo-random-number generator
|
|
|
|
uint32_t rnd;
|
|
|
|
|
2022-04-19 19:23:30 -04:00
|
|
|
l_rndMutex.lock(); // <--- lock the shared random seed
|
2015-12-31 14:56:37 -05:00
|
|
|
// "Super-Duper" Linear Congruential Generator (LCG)
|
|
|
|
rnd = l_rnd * (3U*7U*11U*13U*23U);
|
|
|
|
l_rnd = rnd; // set for the next time
|
2022-04-19 19:23:30 -04:00
|
|
|
l_rndMutex.unlock(); // <--- unlock the shared random seed
|
2015-12-31 14:56:37 -05:00
|
|
|
|
|
|
|
return rnd;
|
|
|
|
}
|