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,534 of 243,242   
   David Brown to Michael Sanders   
   Re: srand(0)   
   23 Dec 25 08:25:59   
   
   From: david.brown@hesbynett.no   
      
   On 23/12/2025 08:17, Michael Sanders wrote:   
   > On Mon, 22 Dec 2025 06:44:42 -0500, James Kuyper wrote:   
   >   
   >> On 2025-12-22 03:48, 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);   
   >>   
   >> No, why whould you think so?   
   >   
   > Excuse my delayed reply James (net provider was down most of today).   
   >   
   > Well, I guess I did not expect such large differences between   
   > gcc & musl somehow (cant test with clang just yet). I understand the   
   > sequence is deterministic & likely still some differences with musl,   
   > yet I wrongly assumed it seems, the sequences would be the same...   
   >   
   > #include    
   > #include    
   >   
   > int main(void) {   
   >      srand(0);   
   >      printf("%d\n", rand());   
   >      return 0;   
   > }   
   >   
   > /*   
   >   
   > $ gcc -O2 -o rnd rnd.c && ./rnd   
   > 1804289383   
   >   
   > $ musl-gcc -static -O2 -o rnd rnd.c && ./rnd   
   > 2049033599   
   >   
   > */   
   >   
      
   It is not the compilers that are different, it is the C standard   
   libraries that are different.  gcc is a compiler, not a library, but you   
   are probably using glibc with it by default.  musl is a library, not a   
   compiler.  There is no reason to suppose that different C standard   
   libraries use the same implementation of rand()and srand(), so no reason   
   to suppose they would give the same sequences - though each on their own   
   will give a deterministic pseudo-random sequence based on their seeds.   
      
   If you swap gcc with clang you will get the same results - it will   
   depend on whether you are linking with glibc, musl, or another C library.   
      
   --- 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