2

I need this code to generate a number between 5 and 8 and then assign it to a variable. However when i make it give me only one number instead of multiple, it only gives me the same number. While when i have it set to give e multiple it gives me random numbers.

#include <random>
#include <iostream>

int main()
{
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dis(5, 8);

    for (int n=0; n<1; ++n)
        std::cout << dis(gen) << ' ';
    std::cout << '\n';
}
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
user3317455
  • 99
  • 1
  • 2
  • 9
  • It looks like you need to set the seed of the `random_device` to a different value each time. `random_device` is allowed to be a PNRG. You can check it by calling it many times, e.g by putting `rd()` in the `cout` loop. – juanchopanza Jun 05 '14 at 05:42
  • duplicated: http://stackoverflow.com/questions/22890738/seeding-the-random-number-generator/22891670#22891670 – harper Jun 05 '14 at 05:44
  • We have srand in it, it still doesn't work. – user3317455 Jun 05 '14 at 05:46
  • [here](http://stackoverflow.com/a/23838367/2567683) – Nikos Athanasiou Jun 05 '14 at 05:48
  • FWIW in my implementation, I get different numbers each time. – juanchopanza Jun 05 '14 at 05:48
  • How can i set the seed of random_device then? – user3317455 Jun 05 '14 at 05:48
  • @harper `rand` and `srand` are (thankfully) entirely separate from the C++11 RNGs. –  Jun 05 '14 at 05:51
  • Can you clarify your question? Do you get the same sequence when looping more than once or not? And what compiler/platform are you using? – juanchopanza Jun 05 '14 at 05:51
  • http://codepad.org/ZSo2fVxU I managed to find this and it works except i need a way for it only to give me one. – user3317455 Jun 05 '14 at 06:02
  • @delnan Despite the implementation in C++11 is completely different the statement about pseudo random numbers and seeding applies entirely. – harper Jun 05 '14 at 06:02
  • 1
    @user3317455 Give you only one what? Random number? Call the distribution once, and it'll give you a single random number. And what compiler/version/platform etc. are you using? – Praetorian Jun 05 '14 at 06:21

2 Answers2

6

1 - You have "only one number" :

Because your for-loop does only one iteration.

for (int n=0; n<1; ++n)
                ^
               Here

2 - If you always see the same pseudo random sequence, this is because the seed is itself pseudo random: for a std::random_device, this is implementation specific :

Note that std::random_device may be implemented in terms of a pseudo-random number engine if a non-deterministic source (e.g. a hardware device) is not available to the implementation.

Note that your example on ideone is not pseudo random and works fine: http://ideone.com/lysKrt

Alternatively to rd, you can use the system time as a seed :

    #include <chrono>
    // ...

    unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
    std::mt19937 gen(seed);
quantdev
  • 23,517
  • 5
  • 55
  • 88
0

It is because the executable always starts from the same seed. If you want different numbers in each run then you need to seed the generator. Say, with the current time.

LaszloLadanyi
  • 973
  • 4
  • 12
  • -1. You should explain how it can be that the same seed is being used. There is an attempt to seed the generator each time. – juanchopanza Jun 05 '14 at 05:54