rand changes value without changing seed

Go To StackoverFlow.com

6

Take the following program:

#include <cstdlib>
using std::rand;

#include <iostream>
using std::cout;

int main()
{
    cout << rand() << ' ' << rand() << ' ' << rand() << '\n';
}

Due to rand producing the same values as long as the seed isn't changed using srand, this should produce three identical numbers.
e.g.

567 567 567

However, when I run this program it gives me three different values.
e.g.

6334 18467 41

When the program is (compiled and) run again, the same three numbers are generated. Shouldn't I have to use srand to change the seed before I start getting different results from rand? Is this just my compiler/implementation trying to do me a favour?

OS: Windows XP
Compiler: GCC 4.6.2
Libraries: MinGW

EDIT: By trying to use srand, I discovered that this is the result from a seed of 1 (which I guess is made default).

2012-04-05 16:17
by chris
+1 for short, complete example program. +1 for showing what the program produces. +1 for showing what you expect it to produce - Robᵩ 2012-04-05 16:20
You're suggesting that the random number generator would need to be seeded before every call to rand? That sounds more like a user generated number than a randomly generated number - Benjamin Lindley 2012-04-05 16:20
If all appearances of rand in a function gave the same result, it'll be not useful at all - hjpotter92 2012-04-05 16:21
@BenjaminLindley, That's the way I always learned how to use this exact function in the past: set the seed to something (eg. time(NULL)) before each call, otherwise the same result was produced - chris 2012-04-05 16:21
@chris: I think you are misremembering your learning. What you were probably told is something more like "you need to seed the random number generator before you start using it, otherwise you will get the same sequence of values every time - Benjamin Lindley 2012-04-05 16:22
FYI: http://c-faq.com/lib/srand.htm - Robᵩ 2012-04-05 16:23
@BenjaminLindley, Perhaps I was. It may have been a mixup with the fact that every time you run the program it gives the same result, rather than every function call. I do believe this is the case. Thanks for jogging my memory - chris 2012-04-05 16:24


4

Each call to rand() will always generate a different random number.

The seed actually determines the sequence of random numbers that's created. Using a different seed will get you another 3 random numbers, but you will always get those 3 numbers for a given seed.

If you want to have the same number multiple times just call rand() once and save it in a variable.

2012-04-05 16:23
by ThomasG
Different calls to rand() can return the same value - David Heffernan 2012-04-05 16:25
This was indeed the case. I was thinking of it differing every call instead of every execution. Granted that indeed a certain seed could mathematically end up generating the same three numbers - chris 2012-04-05 16:27
David you're absolutely right, I just figured it's rare enough that it wasn't worth mentioning for this. I should've said that it always is (pseudo)random, whether it's actually the same number or not - ThomasG 2012-04-05 16:33


4

Calling rand() multiple times intentionally produces a different random number each time you call it.

Unless your program calls srand() with a different value for each run, the sequence will be the same for each run.

You could use srand() with the time to make the entire sequence different each time. You can also call srand() with a known value to reset the sequence - useful for testing.

See the documentation for rand() and srand().

2012-04-05 16:22
by RichieHindle
rand() could return the same number 3 times in a row - David Heffernan 2012-04-05 16:24
That was my problem. I have to give credit to ThomasG for being the first to post an answer explaining the mistake in my thought process though - chris 2012-04-05 16:28
Ads