Replace C rand() with uniform_int_distribution and mt19937. (#2089)

Reuse random engine and distribution itself and change only
distribution bounds via lightweight param type.
This commit is contained in:
Dzmitry Neviadomski 2021-11-11 22:03:33 +03:00 committed by GitHub
parent 53f799ada6
commit 319e1fc6bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 9 deletions

View File

@ -7,8 +7,8 @@
*/
#include <cstdarg>
#include <cstdlib> /* abs(), srand(), rand() */
#include <cstring> /* memcpy(), memmove(), memset(), strcmp(), strlen() */
#include <random> /* random_device, mt19937, uniform_int_distribution*/
#include <arc4.h>
@ -106,15 +106,12 @@ int tr_rand_int_weak(int upper_bound)
{
TR_ASSERT(upper_bound > 0);
static bool init = false;
thread_local auto random_engine = std::mt19937{ std::random_device{}() };
using distribution_type = std::uniform_int_distribution<>;
thread_local distribution_type distribution;
if (!init)
{
srand(tr_time_msec());
init = true;
}
return rand() % upper_bound;
// Upper bound is inclusive in std::uniform_int_distribution.
return distribution(random_engine, distribution_type::param_type{ 0, upper_bound - 1 });
}
/***