lundi 28 décembre 2015

C++11: How to set seed using

I am exercising the random library, new to C++11. I wrote the following minimal program:

#include <iostream>
#include <random>
using namespace std;
int main() {
    default_random_engine eng;
    uniform_real_distribution<double> urd(0, 1);
    cout << "Uniform [0, 1): " << urd(eng);
}

When I run this repeatedly it gives the same output each time:

>a
Uniform [0, 1): 0.131538
>a
Uniform [0, 1): 0.131538
>a
Uniform [0, 1): 0.131538

I would like to have the program set the seed differently each time it is called, so that a different random number is generated each time. I am aware that random provides a facility called seed_seq, but I find the explanation of it (at cplusplus.com) totally obscure:

http://ift.tt/1MCJ9b3

I'd appreciate advice on how to have a program generate a new seed each time it is called: The simpler the better.

Aucun commentaire:

Enregistrer un commentaire