wwwolf wrote:
Johannes Bauer wrote:
Do something like
srand(time(NULL));
Is there any difference at all between srand(time(NULL)); and srand(time(0));?
Well, yes. 0 is the number zero. NULL is the pointer zero, usually
defined as "(void*)0". The time(2) call is system specific; however on
my box, a Linux system, it demands a "time_t*" as an argument. It
requires a pointer. NULL is a pointer, 0 is not.
So when you want to go safe, use NULL, although "0" might work when your
compiler doesn't take type-safety too seriously (which _you_ should).
you should
follow the advice from rand(3) how to avoid using lower-order bits in
your random numbers.
Where is this advice, and how do I get it?
Try typing "man 3 rand" - when you've manual pages installed it comes
up. When you don't, here it is:
------------
In Numerical Recipes in C: The Art of Scientific Computing (William
H. Press, Brian P. Flannery, Saul A. Teukolsky, William T. Vetterling;
New York: Cambridge University Press, 1992 (2nd ed., p. 277)), the
following comments are made:
"If you want to generate a random integer between 1 and 10, you should
always do it by using high-order bits, as in
j=1+(int) (10.0*rand()/(RAND_MAX+1.0));
and never by anything resembling
j=1+(rand() % 10);
(which uses lower-order bits)."
------------
Greetings,
Johannes
--
PLEASE verify my signature. Some forging troll is claiming to be me.
My GPG key id is 0xCC727E2E (dated 2004-11-03). You can get it from
wwwkeys.pgp.net or random.sks.keyserver.penguin.de.
Also: Messages from "Comcast Online" are ALWAYS forged.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Thunderbird -
http://enigmail.mozdev.org
iD8DBQFCWJKiCseFG8xyfi4RAmMyAJ9Hg+plvUgD+h3WmnWoEF JBzq/KmwCgkA4P
ULAG03W9y3zzeO4HlIq4Hg0=
=Nskj
-----END PGP SIGNATURE-----