This question is a direct follow-up of:
Have a look at this Mersenne Twister random-number generator function
Thanks to Code Review and Stack Overflow, I now realize, that:
-
unsigned long long int
is not guaranteed to be 64-bit -
I don’t want to re-initialize the
random_device
on every function call
and a few other things.
So, based on current information, I’ve re-written it as follows:
#include <iostream> // std::cout #include <cstdint> // std::uint64_t #include <random> // std::random_device, etc. std::uint64_t random_integer(std::uint64_t rand_min, std::uint64_t rand_max) { // use Mersenne Twister as random-number generator engine // initialize = seed the random device; static thread_local = only once // avoid "most vexing parse" by using "uniform initialization syntax" = { something{}() } static thread_local std::mt19937_64 random_number_generator{std::random_device{}()}; // uniform number distribution, guaranteed unbiased std::uniform_int_distribution<std::uint64_t> number_distribution(rand_min, rand_max); // return one random number return number_distribution(random_number_generator); } // this is just to test the output int main() { for (int i = 1; i <= 10; i++) { std::cout << random_integer(10000000, 100000000) << std::endl; } return 0; }
If you could guide me further on improving it, I would be grateful.