1

Possible Duplicate:
Why do I always get the same sequence of random numbers with rand()?

I tried to implement the following generator class for random integers from a given range:

class RandomInteger { protected: std::default_random_engine randomEngine; std::uniform_int_distribution<> distribution; public: RandomInteger(int64_t lower, int64_t upper); virtual ~RandomInteger(); virtual int64_t generate(); }; RandomInteger::RandomInteger(int64_t lower, int64_t upper) : distribution(lower, upper) { } RandomInteger::~RandomInteger() { // TODO Auto-generated destructor stub } int64_t RandomInteger::generate() { int64_t i = this->distribution(this->randomEngine); return i; } 

It produces integers in the range, BUT the sequence of values it produces is the same each time - not very random. Why?

5
  • 6
    Can you also post the code where you set the seed? Commented Jan 10, 2013 at 18:11
  • 4
    @honk I'm guessing there isn't any, which would be the problem. Commented Jan 10, 2013 at 18:12
  • // TODO Auto-generated destructor stub Whatever is auto-generating that for you is doing you quite a disservice... Commented Jan 16, 2013 at 19:14
  • @ildjarn This is Eclipse CDT. Why exactly is it a disservice? Commented Jan 16, 2013 at 22:10
  • Because having a user-declared destructor changes the triviality of your type. Never declare special members when the compiler-generated implementation will do. Commented Jan 16, 2013 at 22:15

1 Answer 1

2

in general a pseudo-random generator takes a seed.

if you never change the seed, you get always the same pseudo-random output/sequence.

edit: I insist on the word pseudo-random (somtimes pseudo-chaos) instead of the abusivily used random

edit2: there should be the c++11 solution to you problem in this other question (look at @R. Martinho Fernandes 's answer)

Sign up to request clarification or add additional context in comments.

13 Comments

Thanks for the only constructive comment. The "exact duplicate" does not give a C++11 solution.
There are interesting links on the quoted question however: stackoverflow.com/questions/1108780/…
@StephaneRolland I also liked stackoverflow.com/questions/7217791/random-numbers-in-c0x?rq=1, but I agree with Griwes that OP just didn't think of the seed.
"Pseudo-chaotic" returns 4,890 results from Google while "pseudo-random" returns 1,980,000. I honestly had never heard of it before, thanks for introducing me to something new.
"pseudochaotique" returns 17000 from Google. I most probably have this bias because what I read was in french. It was with Iannis Xenakis about his Stochastic Synthesis, and with french books about Chaos Theory... As a consequence, in english it's probably more correct to say pseudo-random. ;-). I re-edit :-)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.