lundi 25 février 2019

C++

I want to generate random numbers outside the main function but even when using the library and seeding the random number generator, the output is not random. Any help is appreciated.

#include <iostream>
#include <random>
#include <time.h>

int foo(std::mt19937 rng)
{
    std::uniform_int_distribution<int> distr(0, 9);

    return distr(rng);
}

int main()
{
    std::random_device rd;
    std::mt19937 rng(rd());

    for (int j=0; j<10; j++)
    {
        std::cout << foo(rng) << " ";
    }
    return 0;
}

With output

5 5 5 5 5 5 5 5 5 5




Aucun commentaire:

Enregistrer un commentaire