I am learning C++, trying to generate a few random numbers within a specified range.
I'm trying to implement this answer here on SO, and revise it for it to be able to generate 64-bit integer.
Which I basically succeeded in, basic program could look like:
#include <iostream>
#include <random>
unsigned long long int random_integer(unsigned long long int rand_min, unsigned long long int rand_max)
{
// initialize = seed the random device
std::random_device random_device;
// use Mersenne Twister as random-number generator engine
std::mt19937_64 random_number_generator(random_device());
// number distribution, guaranteed unbiased
std::uniform_int_distribution<unsigned long long int> number_distribution(rand_min, rand_max);
return number_distribution(random_number_generator);
}
int main()
{
for (int i = 1; i <= 10; i++)
{
std::cout << random_integer(10000000, 100000000) << std::endl;
}
return 0;
}
Questions:
-
should I define the random device:
std::random_device random_device
out of the function?
-
I am also unsure where to define the Mersenne Twister:
std::mt19937_64 random_number_generator(random_device());
for the function to work as expected.
Aucun commentaire:
Enregistrer un commentaire