title: glibc rand function implementation
body: <p>I'm reading <a href="https://github.com/lattera/glibc/blob/master/stdlib/random_r.c" rel="noreferrer">c standard library rand() function implementation</a> with glibc source code. 
  <strong>stdlib/random_r.c, line 359</strong></p>
  
  <pre><code>int
  __random_r (buf, result)
              struct random_data *buf;
              int32_t *result;
  {
    int32_t *state;
  
    if (buf == NULL || result == NULL)
      goto fail;
  
    state = buf-&gt;state;
  
    if (buf-&gt;rand_type == TYPE_0)
      {
        int32_t val = state[0];
        val = ((state[0] * 1103515245) + 12345) &amp; 0x7fffffff;
        state[0] = val;
        *result = val;
      }
    else
      {
        int32_t *fptr = buf-&gt;fptr;
        int32_t *rptr = buf-&gt;rptr;
        int32_t *end_ptr = buf-&gt;end_ptr;
        int32_t val;
  
        val = *fptr += *rptr;
        /* Chucking least random bit.  */
        *result = (val &gt;&gt; 1) &amp; 0x7fffffff;
        ++fptr;
        if (fptr &gt;= end_ptr)
          {
            fptr = state;
            ++rptr;
          }
        else
          {
            ++rptr;
            if (rptr &gt;= end_ptr)
              rptr = state;
          }
        buf-&gt;fptr = fptr;
        buf-&gt;rptr = rptr;
      }
    return 0;
  
   fail:
    __set_errno (EINVAL);
    return -1;
  }
  </code></pre>
  
  <p>I don't understand how random_r generate random number when <code>(buf-&gt;rand_type != TYPE_0)</code>, anyone please explain? Thanks.</p>