tags:

views:

138

answers:

6

This code

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main ()
{
    printf ("First number: %d\n", rand() % 100);
    srand ( time(NULL) );
    printf ("Random number: %d\n", rand() % 100);
    srand ( 1 );
    printf ("Again the first number: %d\n", rand() %100);

    return 0;
}

has the following output:

First number: 41
Random number: 13
Again the first number: 41

There is also the following rule:

Two different initializations with the same seed, instructs the pseudo-random generator to generate the same succession of results for the subsequent calls to rand in both cases.

I understand the words but I just don't understand the method itself. Why did it return 41 again? Is it random or must it return the same result in every case according to this code?

A: 

I think this one may be a problem:

If seed is set to 1, the generator is reinitialized to its initial value and produces the same values as before any call to rand or srand.

srand reference

vodkhang
+2  A: 

If you call rand() without first calling srand(), the behavior is as if you called srand() with 1 as the argument.

This behavior is defined in the original C standard. I don't have a full copy of it handy, but The Open Group's POSIX standards are the next best thing, as they incorporate the full C standard (with some extensions):

http://www.opengroup.org/onlinepubs/000095399/functions/rand.html

The srand() function uses the argument as a seed for a new sequence of pseudo-random numbers to be returned by subsequent calls to rand(). If srand() is then called with the same seed value, the sequence of pseudo-random numbers shall be repeated. If rand() is called before any calls to srand() are made, the same sequence shall be generated as when srand() is first called with a seed value of 1.

The actual result of a rand() call for any given seed is implementation-defined, except that for any seed x, call n to rand() after the seeding of the generator must return the same result. So while on your implementation you will always get 41 on the first rand() call after calling srand(1), and you will always get the same result (whatever it is) for the second rand() call, other implementations may use a different algorithm that produces different results.

Nicholas Knight
A: 

Calling srand with a value of 1 resets the generator to its initial state when the program started.

Marcelo Cantos
+6  A: 

The C++ standard defers to the C standard for rand and srand (see section 26.8 of C++0x):

The rand function has the semantics specified in the C standard, except that the implementation may specify that particular library functions may call rand.

The C standard (7.20.2.2 of C99) states quite categorically:

If rand is called before any calls to srand have been made, the same sequence shall be generated as when srand is first called with a seed value of 1.

So the first time you call rand, the seed is 1. It's also 1 the third time you call it which is why you get the same value.

The second time that you called rand, the seed had been set based on the current time, which is why you got a different result. It's usually not a good idea to re-seed the random number generator unless you actually want the same sequence (such as in testing).

paxdiablo
A: 

srand(3) lets you get repeatable output from the random number generator. This way, you can write programs that depend on random numbers, but test them deterministically. (Typically by having a command-line option to call srand() with a supplied argument, so it could be run a hundred times with 1, 2, 3, 4, 5, ... as inputs.)

So this is behaving as expected.

sarnold
A: 

It's probably because in your initial call to rand the random number generator has not been seeded so it uses a default value of 1 to seed it.

sashang