C++ rand () not providing correct number based on seed
Asked Answered
P

3

5

I am working on a C++ assignment for my class, and we have to use rand () with a seed of 99 to produce a set of values. However, my problem is that when I try to create a value within our parameters, the number is different than what the instructor provided us for a definite first number. The code is shown below:

    int lottoNumber;
  srand (RANDOM_NUMBER_SEED);
  do
  {
    lottoNumber = rand ();
  } while (lottoNumber > 25 || lottoNumber < 1);

  cout << lottoNumber << endl;

The value produced from this is 13, while the number expected to be produced is 2. Any help as to why this is different would be great, thanks!

Point answered 11/2, 2017 at 0:35 Comment(1)
You can probably get rid of the loop altogether, and just use lottoNumber = rand() % 25 + 1;. It may not be perfectly distributed but it should do for anything other than a real lottery but if it were a real lottery, I suspect government regulations would require something better than a PRNG :-)Remmer
S
9

Algorithm used by rand() is implementation-defined.

Which means that it might be different on different compilers and compiler versions.

Socialminded answered 11/2, 2017 at 0:37 Comment(0)
D
5

Your instructor is hallucinating. This is a pseudo-random number generator. Just because the properties of such a generator tend to produce the same results given the same seed, on any particular invocation of a program, doesn't mean that this behaviour is in any way guaranteed, or that the same property should be expected to apply in general forever.

If you want a deterministic sequence, do not use a random number generator!!!

Dutch answered 11/2, 2017 at 0:37 Comment(13)
The rand() random number generator is typically entirely deterministic, depending on the value set by srand(), In fact, this is one of its few useful features.Liu
@NeilButterworth: The OP has discovered, in fact, and quite rightly, that this is not true.Dutch
Deterministic != All implementations produce same resultsLiu
@NeilButterworth: No, that's precisely what it means.Dutch
An instance of a PRNG may be deterministic given the same seed (though it's by no means required in general, the ISO C standard mandates this). The standard does not require determinism across all implementations. It may be the educator and student are using different implementations.Remmer
No, it isn't. Deterministic means given a fixed seed value via srand, an implementation will always produce the same sequence when calls to rand are made.Liu
@NeilButterworth: And the standard does not require any such thing. Not even close. Feel free to write your own answer that explains the OP's problems in different terms. Instead of just being negative. I note that you didn't bother making the same observation on any of the other answers here that say the exact same thing.Dutch
@Lightness "The rand() random number generator is typically entirely deterministic" - notice typically. " Instead of just being negative" - what???Liu
Guys, it sounds like you're arguing but you actually saying the same thing :-) The standard (C, to which C++ defers in this case) states that "if srand is then called with the same seed value, the sequence of pseudo-random numbers shall be repeated". But I'm pretty certain that applies only to a given implementation of C, not across all implementations.Remmer
Having said that, Lightness, I would consider a slight mod, to wit, "particular invocation of a program on different implementations ..."Remmer
@paxdiablo: That's a good quote to focus on, though I believe it refers to a particular invocation rather than across all compiled programs produced by a single implementation. After all, as far as the standard is concerned, an implementation at time X may not be identical to an implementation at time Y even if it happens to be e.g. the same version/build of GCC (the standard doesn't "know" that GCC is compiled and that the executable thereafter doesn't change, and that its code generation does not change over time). But in this I am in guesswork territory to some degree.Dutch
And, FWIW, I have no intention of arguing. If someone wants to produce an alternative theory they are more than welcome to do so. In a competing answer.Dutch
Hmm, you may be right there, that's certainly a valid reading as well.Remmer
J
4

rand() is implementation dependent, from cppreference.com std::rand:

There are no guarantees as to the quality of the random sequence produced.

You might want to use something more like std::mt19937

Jessalin answered 11/2, 2017 at 0:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.