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,522 of 243,242   
   Kaz Kylheku to Janis Papanagnou   
   Re: srand(0)   
   22 Dec 25 19:16:06   
   
   From: 046-301-5902@kylheku.com   
      
   On 2025-12-22, Janis Papanagnou  wrote:   
   > On 2025-12-22 18:13, James Kuyper wrote:   
   >> On 2025-12-22 07:18, Janis Papanagnou wrote:   
   >>> On 2025-12-22 12:44, 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?   
   >>>   
   >>> There's number sequence generators that produce 0 sequences if seeded   
   >>> with 0. ...   
   >>   
   >> The details of how the seed affects the random number sequence are   
   >> unspecified by the standard. I personally would consider a pseudo-random   
   >> number generator to be quite defective if there were any seed that   
   >> produced a constant output.   
   >   
   > I wouldn't have mentioned that if there weren't a whole class of   
   > such functions that expose exactly that behavior by design. Have   
   > a look for PN-(Pseudo Noise-)generators and LFSR (Linear Feedback   
   > Shift Registers).   
      
   The entire class of LFRS's is not homogeneous in this way; it   
   is possible for a Linear Boolean function to generate a nonzero   
   out of zero inputs.   
      
   The LFSR page in the Wikipedia references this definition:   
      
   https://en.wikipedia.org/wiki/Linearity#Boolean_functions   
      
   A Boolean linear function of the inputs x1, ... xn is   
   a kind of polynomial with coefficients C0, C1, ... CN combined   
   with the inputs using AND for multiplication, and XOR for addition:   
      
     f(x1, ... xn) = C0 ^ (C1 & x1) ^ (C2 & x2) ^ .. ^ (Cn ^ xn)   
      
   If c0 is chosen as 1, then we get a 1 output when all the x's are   
   zero. (The article uses "b" for the parameters and "a" for the   
   coefficients, which I'm not crazy about.)   
      
   Also see the two conditions, given in the description, one of which has   
   to be true for Boolean linearity. For condition (2) the remark is given   
   that under that condition f(F, F, F, ... F) = T.   
      
   So if we have a "condition 2" linear function, our LSFR avoids   
   the behavior.   
      
   I don't recall having paid attention to this exact material in the   
   past so it is a "TIL" for me.   
      
   > #include    
   >   
   > int main ()   
   > {   
   >      uint32_t init = 0x00000038;   
   >      uint32_t reg = init;   
   >      uint32_t new_bit;   
   >      int count = 0;   
   >      do {   
   >          new_bit = ((reg >> 2) + (reg >> 4) + (reg >> 6) + (reg >> 30))   
   > & 0x1;   
      
   These could of course be XOR, without it making a difference; the least   
   significant bit in a binary addition is the XOR of the LSB's of the   
   inputs.   
      
   >          reg <<= 1;   
   >          reg |= new_bit;   
      
   So to have that C0 = 1 coefficient in the linear function, we just make   
   it "reg |= (new_bit ^ 1);".   
      
   --   
   TXR Programming Language: http://nongnu.org/txr   
   Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal   
   Mastodon: @Kazinator@mstdn.ca   
      
   --- 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