public inbox for libc-help@sourceware.org
 help / color / mirror / Atom feed
* What is the reason for multiplying 10 in __srandom_r?
@ 2018-05-31  3:04 Xinzhen Chen
  2018-05-31 15:14 ` Patrick McGehearty
  0 siblings, 1 reply; 2+ messages in thread
From: Xinzhen Chen @ 2018-05-31  3:04 UTC (permalink / raw)
  To: libc-help

Hello, glibc developers :-)

I was trying to understand what functions like rand() do under the hood. I
compiled glibc 2.27 from source and link the new compiled libc.so.6 to my
test C program in the following:

#include <stdlib.h>
#include <stdio.h>

int main(int argc, char const* argv[])
{
int randt[32] = {0};
initstate(2, (char*)randt, 128);
setstate((char*)randt);
printf("%d\n", rand());
return 0;
}

I used gdb to step into initstate() function and got confused when I
stepped into the following line of  __srandom_r function:

  kc = buf->rand_deg;
  for (i = 1; i < kc; ++i)
    {
      /* This does:
   state[i] = (16807 * state[i - 1]) % 2147483647;
but avoids overflowing 31 bits.  */
    }

  buf->fptr = &state[buf->rand_sep];
  buf->rptr = &state[0];
  kc *= 10;
  while (--kc >= 0)
    {
      int32_t discard;
      (void) __random_r (buf, &discard);
    }

I also read the comment of  __srandom_r function which says "Lastly, it
cycles the state information a given number of times to get rid of any
initial dependencies introduced by the L.C.R.N.G.".

My questions are:
1. Why the " given number of times" is "kc * 10" instead of "kc * 8" or "kc
* 16" which is more efficient in multiplication?
2. Are there papers or books which dive into glibc random implementation
?(I've read some chapters of <TAOCP vol 2>, <Advanced mordern algebra>,but
still can not totally understand the implementation of glibc random
functions)

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-05-31 15:14 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-05-31  3:04 What is the reason for multiplying 10 in __srandom_r? Xinzhen Chen
2018-05-31 15:14 ` Patrick McGehearty

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).