3

My understanding is that a pseudo-random number generator basically just takes some number (the seed), hashes it with a bunch of XORs and bitshifts, and then spits out a really long number from which a remainder can be retrieved to get your "random" number.

Now, usually you'd use time(NULL) as the seed for rand() in C/C++. However, time(NULL) only increments every second, not every millisecond. So how, then, can I for loop over rand() a thousand times in less than one second and still get different numbers as outputs if the seed is still the same time(NULL) value?

Andrew_CS
  • 2,542
  • 1
  • 18
  • 38
Coffee Maker
  • 1,543
  • 1
  • 16
  • 29
  • 9
    Call `srand(time(NULL));` before the call to `rand()` within the loop, and you'll see the output never changes for a second. This is why you should usually only seed an RNG once at the beginning of your program. Also, C++11 added a bunch of better RNGs in the [``](http://en.cppreference.com/w/cpp/numeric/random) header. You should use those if your compiler supports them. – Praetorian Jun 24 '14 at 18:34
  • 2
    If you are interested in psuedo-random number generators, take a look at the immortal "The Art of Computer Programming", by Donald Knuth. There's an extensive coverage of that topic. – Zaphod Beeblebrox Jun 24 '14 at 18:45

3 Answers3

11

rand() uses the previous random value as the new seed on subsequent calls. This is why a unique random sequence of values will be generated when you start with a different seed value.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
  • 4
    While it is the most clear, I don't think it's necessarily (or effectively) technically accurate. The RNG has state, and each time you call `rand` it changes to the "next" state, and generates a pseudo-random number from the current state. – Mooing Duck Jun 24 '14 at 18:46
  • "Uses the previous random value as the new seed." At least in the sense that seeding means calling `srand()`, [that does not hold](http://coliru.stacked-crooked.com/a/c537c22b36b412b0). – Baum mit Augen Jun 24 '14 at 18:46
  • 1
    @MooingDuck Yes, technically only the initial value is called a "seed." It simplifies the explanation to think of the random value generated as the new seed for subsequent calls though, since computationally the random value is used in exactly the same way as the seed is on the first call. (At least for a linear congruential generator.) – Bill the Lizard Jun 24 '14 at 18:52
  • @BilltheLizard If this is the case, is `rand()` not a LCG or is my [example](http://coliru.stacked-crooked.com/a/c537c22b36b412b0) wrong? – Baum mit Augen Jun 24 '14 at 18:57
  • @BaummitAugen I'm not sure. I can't see anything wrong with your code. I would expect the printed values to be the same, but I don't know the implementation details of `rand()`. It might be worth posting a separate question to see if anyone can explain the output of your code. – Bill the Lizard Jun 24 '14 at 19:19
  • @BaummitAugen The fact that `RAND_MAX` varies from platform to platform makes it clear that there is no fixed specification for the implementation of `rand()`. lcg's seem to be fairly common as a class of implementations, but can have [completely different parameterizations](http://en.wikipedia.org/wiki/Linear_congruential_generator#Parameters_in_common_use). – pjs Jun 24 '14 at 21:24
2

A pseudo number generator outputs a deterministic series of numbers in a certain range that are supposed to look random.

The time(NULL) is the so called seed of the RNG and tells it, where in the series to start. You should only do this once per program.

By the way, rand() is not modern C++. See here for why and what to do instead.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
0

It's common for C / C++ compilers to use a linear congruential generator for rand().

rcgldr
  • 27,407
  • 3
  • 36
  • 61
  • True, which is one of the reasons that using `rand()` is [strongly discouraged](https://www.securecoding.cert.org/confluence/display/seccode/MSC30-C.+Do+not+use+the+rand()+function+for+generating+pseudorandom+numbers). – pjs Jun 24 '14 at 21:29