From: porkchop@invalid.foo   
      
   On Wed, 07 Jan 2026 13:54:21 -0800, Keith Thompson wrote:   
      
   > 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.   
      
   So then clang would use:   
      
   #ifdef __OpenBSD__   
    srand_deterministic(seed);   
   #else   
    srand(seed);   
   #endif   
      
   But I don't know (yet) that gcc does as well under OpenBSD.   
      
   --   
   :wq   
   Mike Sanders   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|