Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 07 Oct 1996 12:57:02 -0600
From:      Warner Losh <imp@village.org>
To:        hackers@freebsd.org
Subject:   rand() and srand()
Message-ID:  <199610071857.MAA08190@rover.village.org>

next in thread | raw e-mail | index | archive | help
>From the rand() man page:
     The rand() function computes a sequence of pseudo-random integers in the
     range of 0 to RAND_MAX (as defined by the header file <stdlib.h>).

     The srand() function sets its argument as the seed for a new sequence of
     pseudo-random numbers to be returned by rand().  These sequences are re-
     peatable by calling srand() with the same seed value.

     If no seed value is provided, the functions are automatically seeded with
     a value of 1.

This tells me that if I give the same seed to srand() three times in a
row, I will get the same pseudo random sequence out of it.

If you are proposing that this change, then you are breaking existing
code's assumptions.  After all, it is documented to do this.

Large numerical applications *MUST* have this ability.  As someone who
helped many grad students and post-docs prepare papers for
publication, these sorts of things were *EXTREMELY* important.  The
large simulations had a test suite that had been hand verified for a
given seed value.  Whenever the simulator needed to be changed, these
test cases were run against the known good results, and any changes in
the output indicated the recent changes were buggy.

The general assumption was that these sequences would be invariant
over time.  Given the man page for the rand() function, I can easily
see how this happened.

Not then, the real problem is that many people are using rand() for
which a better, less pseudo-random approach is required.  Fixing
rand() for these people is a mistake, given the long history rand()
has in the numerical community.

Warner

P.S.  I can likely go on for hours on why you must get the same
sequence of numbers for the same seed, and why that sequence can't
change over time.

P.P.S.  rand() is not suitable for cryptographic applications.
Anybody that uses rand() for those applications is an idiot, since
pseudo-random numbers aren't cryptographically random.



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199610071857.MAA08190>