random always returns same values

Hi
I was trying to use the new c++ random classes but for some reason the output of the program is always the same. I tried to put Sleep() in there to make difference but the output remains the same.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26


#include <iostream>
#include <string>
using namespace std;
#include <random>
#include <functional>

#include <Windows.h>

int main()
{
std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(2, 5);

auto dice = std::bind(distribution, generator);

Sleep(1000);

cout << dice() << " " << dice() << " " << dice() << endl; // output: 4 4 2


 return 0;
}



Output is: 4 4 2

Anyidea whats wrong?
Last edited on
You did not seed your RNG.

std::default_random_engine generator( std::random_device{}() );
Thank you

Last edited on
Additionally I would not suggest to use default_random_engine : it is usually implements a bad RNG. Use std::mt19937 for pretty good mersenne twister generator.
can I also ask what is the std::random_device{}() syntaxt about? I've never seen {} being used like that before.
Last edited on
I create a temporary object of type std::random_device by using uniform initialization: std::random_device{}. Then I call operator() on that temporary object: std::random_device{}()
Ok makes sense, thanks
Topic archived. No new replies allowed.