2

srand(0) and srand(1) give the same results. srand(2), srand(3), etc. give different results.

Any reason why seed = 0 and seed = 1 yield the same random sequence?

Can't find an explanation in the man page. Only that if a seed is not provided, seed = 1 is used.

Thanks.

João M. S. Silva
  • 1,078
  • 2
  • 11
  • 24
  • 1
    Apparently 0 means no seed and, as you state, leads to seed == 1. – mah Dec 09 '14 at 18:48
  • why they should be different? – yeyo Dec 09 '14 at 18:48
  • @mah: I think no seed means `srand()` was not called. – João M. S. Silva Dec 09 '14 at 19:44
  • OP: please try searching first. If you'd typed `srand` into the search box, the first result would have answered your question. – DoxyLover Dec 09 '14 at 19:53
  • @DoxyLover: I searched before and during the posting (among the suggestions) but with a search string that did not return the question you mention and which this one is a duplicate of. I also searched with Google. What seems obvious is not always right. – João M. S. Silva Dec 09 '14 at 21:06
  • @JoãoM.S.Silva since the statement came from the srand() man page, that would seem a bit silly (but I'll agree that the statement itself is somewhat strange since the function has a required argument). Beyond that, considering the source review provided by slugonamission I think you should perhaps reconsider your thought though. – mah Dec 10 '14 at 12:43
  • @mah: I can't fully understand what you say, but the man page for srand is the same for rand and rand_r, and what it says is that "If no seed value is provided, the rand() function is automatically seeded with a value of 1". So I was led to think that `srand(1)` was the default but `srand(x)` with `x` other than 1 would lead to a different result. – João M. S. Silva Dec 10 '14 at 15:40

4 Answers4

11

Within the glibc sources for srandom_r (which is aliased to srand), line 179:

/* We must make sure the seed is not 0.  Take arbitrarily 1 in this case.  */
if (seed == 0)
    seed = 1;

It's just an arbitrary decision basically.

slugonamission
  • 9,562
  • 1
  • 34
  • 41
1

Depends on compiler!

srand(0);
int a=rand(),b=rand();
srand(1);
int c=rand(),d=rand();

VC 2005 result:

a    0x00000026    int
b    0x00001e27    int
c    0x00000029    int
d    0x00004823    int
Anonymous
  • 2,122
  • 19
  • 26
1

This is an implementation dependent behaviour.

For instance, POSIX.1-2001 gives the following example of an implementation of rand() and srand()

static unsigned long next = 1;

/* RAND_MAX assumed to be 32767 */
int myrand(void) {
    next = next * 1103515245 + 12345;
    return((unsigned)(next/65536) % 32768);
}

void mysrand(unsigned seed) {
    next = seed;
}

Now, if you use this implementation you will end up with:

0
16838

for srand(0) and srand(1) respectively.

ref.: http://linux.die.net/man/3/rand

I ran into a quite similar problem before, where rand() yielded different sequences for the same seed across different platforms. Lesson learned, portable code should implement his own PRNG.

yeyo
  • 2,954
  • 2
  • 29
  • 40
-2

The function srand() is used to initialize the pseudo-random number generator by passing the argument seed.

So if the seed is set to 1 then the generator is reinitialized to its initial value. Then it will produce the results as before any call to rand and srand.

so srand(1) actually represent the result srand(0).

  • 2
    Fantastic, but how does this in any ay address the OP's question of **why** both 0 and 1 produce the **same** prng sequence? – WhozCraig Dec 09 '14 at 18:52