home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c      Meh, in C you gotta define EVERYTHING      243,242 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 242,573 of 243,242   
   Ike Naar to John McCue   
   Re: srand(0)   
   24 Dec 25 15:22:47   
   
   From: ike@sdf.org   
      
   On 2025-12-23, John McCue  wrote:   
   > Ike Naar  wrote:   
   >> On 2025-12-23, John McCue  wrote:   
   >>> 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));   
   >>   
   >> srand takes an unsigned argument.   
   >>   
   >>   unsigned s;   
   >>   read(fd, &s, sizeof s);   
   >   
   > I am not quite sure what you are saying about srand(3).   
   >   
   > If you decide to read /dev/urandom, there is no need to   
   > call srand(3), the OS maintains random data itself.  So   
   > read(2) will just return the random number of the type   
   > you want based upon the call.   
      
   Sorry, my misunderstanding. I thought you wanted to use a random   
   number (from /dev/urandom instead of time(NULL)) to seed srand().   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca