0

I want to randomly select an integer among n numbers, where n is small (say 5). To do this, I am using std::uniform_int_distribution.

The exact details of the distribution are not that important, but I want the results to be random and to use all the integers in the range. The randomized selection will be done many more times that the length (size) of the range. I want to the choice to be done quickly and randomly.

But, no matter how many times I run it, I receive the same output. In an attempt to get different output, I let some minutes to pass and then tried again: the result was still the same (probably this code is not affected by time).

Is it expected that each time I use uniform_int_distribution that I will get the same output?

As uniform_int_distribution in particular is not what I need (I just need random numbers), I tried with RandomUniform from my pseudosite. While I got different results than std::uniform_int_distribution produced, when I ran it again it produced the same numbers each time. Of course, I used the same main.

I didn't post RandomUniform, in order to not make the post any bigger.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 2
    Every time you run your program you are using the same seed for the rng. So getting the same output is expected. You need to seed the rng with some that is expected to be different, time for example. – pinkfloydx33 May 10 '14 at 17:18
  • Thank you for the reply @pinkfloydx33, wish I couldn't only upvote your comment. – gsamaras May 10 '14 at 17:27
  • I got a downvote to my question. Why? Should I delete it? – gsamaras May 10 '14 at 17:34
  • Please do not include the answer in your question. It's a question, after all. – Nic Jun 29 '16 at 01:59
  • @QPaysTaxes thanks, but I see the answers and what I have in my question and it's not clear what I should post. :/ – gsamaras Jun 29 '16 at 17:09
  • @gsamaras What you should keep is your question and everything related to it. For example, your question is "Why do I keep getting the same numbers for the generator and how can I fix it"; the answer is "seed the generator". Everything that's "seed the generator" or part of the explanation for that should be removed; everything that's part of your actual question (i.e. your question, what you've tried, what happens, and what you expect) should stay in. – Nic Jun 29 '16 at 18:15
  • @QPaysTaxes I provided an answer following your guidelines, hope you like it. – gsamaras Jun 30 '16 at 14:51

4 Answers4

8

Try to seed the random engine. Time is a good choice. Read this to get familiar to another ways.

std::default_random_engine generator( (unsigned int)time(0) );

or you can use std::random_device which tries to produce non-deterministic random numbers

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

std::random_device is a uniformly-distributed integer random number generator that produces non-deterministic random numbers.

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.

It uses a hardware random generator device or generates a pseudo random number. Useful to seed.

masoud
  • 55,379
  • 16
  • 141
  • 208
  • 1
    Thanks M²! I think you could improve your answer by explaining a bit what `random_device` is. In the link is not obvious. – gsamaras May 10 '14 at 17:28
  • I got a downvote to my question. Why? Should I delete it? Thanks for the explanation. – gsamaras May 10 '14 at 17:34
  • @G.Samaras: No, you can't because the question has answers. You can improve your post to convince the downvoter to remove his downvote. (BTW, I don't know what is the reason of that) – masoud May 10 '14 at 17:36
  • I think that there will be a guy who will have the same problem with me. I would like him to find what he is looking for. I will edit, but only a bit. Thanks again. – gsamaras May 10 '14 at 17:39
3

You didn't seed your default_random_engine, so it's using the default, constant, seed.

T.C.
  • 133,968
  • 17
  • 288
  • 421
  • You are correct and the first to answer. However, the answer of M² is more complete, since he says how to seed. That's why I will accept that answer. I think it is fair. :) – gsamaras May 10 '14 at 17:29
1

As others have mentioned, your main problem is that you're not seeding your engine. I thought I'd address something else since you note that speed and quality is important to you.

Unlike other engines, default_random_engine makes no guarantees. Do not use it if you have any reasonable requirement. It's only there if you don't know what you're doing and the application of it doesn't really matter. From the standard:

Remark: The choice of engine type named by this typedef is implementation-defined. [ Note: The implementation may select this type on the basis of performance, size, quality, or any combination of such factors, so as to provide at least acceptable engine behavior for relatively casual, inexpert, and/or lightweight use. Because different implementations may select different underlying engine types, code that uses this typedef need not generate identical sequences across implementations. — end note ]

A better choice might be mt19937, which has high quality and is very fast.

Cory Nelson
  • 29,236
  • 5
  • 72
  • 110
  • How to use it? Reference isn't helping. Like here http://stackoverflow.com/questions/11817493/random-number-generation-best-practices in the 3rd answer? An example would improve this answer, IMHO. – gsamaras May 10 '14 at 17:37
  • +1, as to every answer here, for the `mt19937`, even though no example provided. – gsamaras May 10 '14 at 19:41
  • Cory, please, I can not make it work! I tried this http://en.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine/seed but it wont't compile! I have c++11 enabled. – gsamaras May 10 '14 at 20:42
1

As said, I should seed the generator. Notice that the reference does not provide something obvious for a seed.

The working code is here.

Notice, that as mentioned in the answers, mt19937 should be used, for better speed and quality.

Example in first answer here.

Here is another example, found on the internet, that compiles.

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305