I need to generate random Boolean values on a performance-critical path.
The code which I wrote for this is
std::random_device rd;
std::uniform_int
iI think that best way is an using of precalculated random array:
uint8_t g_rand[UINT16_MAX];
bool InitRand()
{
for (size_t i = 0, n = UINT16_MAX; i < n; ++i)
g_rand[i] = ::rand() & 1;
return true;
}
bool g_inited = InitRand();
inline const uint8_t * Rand()
{
return g_rand + (::rand()&INT16_MAX);
}
It using to fill some array dst[size]:
const size_t size = 10000;
bool dst[size];
for (size_t i = 0; i < size; i += INT16_MAX)
memcpy(dst + i, Rand(), std::min(INT16_MAX, size - col));
Of course you can initialize pre-calculated array with using of another random function.