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?

Community
  • 1
  • 1
clstaudt
  • 21,436
  • 45
  • 156
  • 239

1 Answers1

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)

Community
  • 1
  • 1
Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
  • Thanks for the only constructive comment. The "exact duplicate" does not give a C++11 solution. – clstaudt Jan 10 '13 at 18:22
  • 1
    There are interesting links on the quoted question however: http://stackoverflow.com/questions/1108780/why-do-i-always-get-the-same-sequence-of-random-numbers-with-rand – Stephane Rolland Jan 10 '13 at 18:28
  • And maybe the c++11 solution to your question on this one I have added in my answer: http://stackoverflow.com/questions/8389989/c-equivalent-of-new-randomseed-in-c-sharp – Stephane Rolland Jan 10 '13 at 18:31
  • @cls, the answer stays the same: *you need to seed it*. The question and the answer are essentially the same here and that C question. – Griwes Jan 10 '13 at 18:36
  • @Griwses but we should admit that the c++11 solution is really less straigth-forward... – Stephane Rolland Jan 10 '13 at 18:39
  • You might replace 'psuedo-random' with 'chaotic' but replacing 'random' with 'psuedo-chaotic' doesn't make sense to me. – bames53 Jan 10 '13 at 18:40
  • 2
    @StephaneRolland I also liked http://stackoverflow.com/questions/7217791/random-numbers-in-c0x?rq=1, but I agree with Griwes that OP just didn't think of the seed. – Benjamin Bannier Jan 10 '13 at 18:40
  • @honk thx for this other link. – Stephane Rolland Jan 10 '13 at 18:43
  • The word "pseudo-random" was coined specifically to apply to programmatic random number generators. I don't know why you feel a need to coin another one. – Mark Ransom Jan 10 '13 at 19:02
  • @MarkRansom I'm fine with **pseudo-random** ;-) Just that when I was learning to generate such sequences, the documents and books I read were using the word pseudo-chaotic, so it's the one that I still use... the important to keep in mind is the word **pseudo** :-) – Stephane Rolland Jan 10 '13 at 19:25
  • I edit since you're not the only one to find that word strange. – Stephane Rolland Jan 10 '13 at 19:27
  • 1
    "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. – Mark Ransom Jan 10 '13 at 19:41
  • 1
    "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 :-) – Stephane Rolland Jan 10 '13 at 19:50