Path: csiph.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: srand(0) Date: Wed, 07 Jan 2026 13:54:21 -0800 Organization: None to speak of Lines: 50 Message-ID: <87ikddgkc2.fsf@example.invalid> References: <10ib0ka$3cgil$1@dont-email.me> <10icocl$3u4ua$1@dont-email.me> <867bttph43.fsf@linuxsc.com> MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Wed, 07 Jan 2026 21:54:22 +0000 (UTC) Injection-Info: dont-email.me; posting-host="6f871b5ffe12a84f74a2becf62529d59"; logging-data="1068708"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+dNJvf3m3Em3VPioxY5uWa" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:H1b4PIeOI5b6+slzKVkqP1oaqFg= sha1:izPiHSyWxa3rkOiKp+wZdx14Rto= Xref: csiph.com comp.lang.c:396283 Tim Rentsch writes: > John McCue writes: >> Michael Sanders wrote: >>> Is it incorrect to use 0 (zero) to seed srand()? >>> >>> int seed = (argc >= 2 && strlen(argv[1]) == 9) >>> ? atoi(argv[1]) >>> : (int)(time(NULL) % 900000000 + 100000000); >>> >>> srand(seed); >> >> I like to just read /dev/urandom when I need a random >> number. Seem easier and more portable across Linux & >> the *BSDs. >> >> int s; >> read(fd, &s, sizeof(int)); > > Apples and oranges. Many applications that use random numbers > need a stream of numbers that is deterministic and reproducible, > which /dev/urandom is not. And neither is the non-conforming rand() on OpenBSD. The rand(1) man page on OpenBSD 7.8 says: Standards insist that this interface return deterministic results. Unsafe usage is very common, so OpenBSD changed the subsystem to return non-deterministic results by default. To satisfy portable code, srand() may be called to initialize the subsystem. In OpenBSD the seed variable is ignored, and strong random number results will be provided from arc4random(3). In other systems, the seed variable primes a simplistic deterministic algorithm. It does provide an srand_deterministic() function that behaves the way srand() is supposed to. And a program that calls rand() produces a link-time warning, even though OpenBSD's rand() *doesn't* return deterministic values. ld: warning: rand_test.c(rand_test.o:(main)): warning: rand() may return deterministic values, is that what you want? (In a similarly questionable decision, OpenBSD's printf triggers a SIGABRT signal if the format string uses "%n".) -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com void Void(void) { Void(); } /* The recursive call of the void */