How can I get the sourcecode for rand() (C++)?

2019-02-06 09:37发布

问题:

I'm new to programming.

I want to know exactly what rand() does.

Searching only yields examples on its usage. But none explain each step of how the function generates a random number. They treat rand() as a blackbox.

I want to know what rand() is doing; each step.

Is there a resource that will allow me to see exactly what rand() does? This is all open source stuff isn't it? I'll settle for the disassembly, if there's no source.

I know it returns a random number, but how does it generate that number? I want to see each step.

Thank you.

回答1:

This was 10 seconds of googling:

  • gcc implementation of rand()
  • How is the rand()/srand() function implemented in C
  • implementation of rand()

...

  • http://gcc.gnu.org/onlinedocs/gcc-4.6.2/libstdc++/api/a01206.html
  • http://www.gnu.org/software/libc/manual/html_node/Pseudo_002dRandom-Numbers.html

I was gonna list the actual search, but seeing this is clearly a dupe, I'll just vote as dupe



回答2:

Here is the current glibc implementation:

/* Return a random integer between 0 and RAND_MAX.  */
int
rand (void)
{
  return (int) __random ();
}

That's not much help, but __random eventually calls __random_r:

/* If we are using the trivial TYPE_0 R.N.G., just do the old linear
   congruential bit.  Otherwise, we do our fancy trinomial stuff, which is the
   same in all the other cases due to all the global variables that have been
   set up.  The basic operation is to add the number at the rear pointer into
   the one at the front pointer.  Then both pointers are advanced to the next
   location cyclically in the table.  The value returned is the sum generated,
   reduced to 31 bits by throwing away the "least random" low bit.
   Note: The code takes advantage of the fact that both the front and
   rear pointers can't wrap on the same call by not testing the rear
   pointer if the front one has wrapped.  Returns a 31-bit random number.  */

int
__random_r (buf, result)
     struct random_data *buf;
     int32_t *result;
{
  int32_t *state;

  if (buf == NULL || result == NULL)
    goto fail;

  state = buf->state;

  if (buf->rand_type == TYPE_0)
    {
      int32_t val = state[0];
      val = ((state[0] * 1103515245) + 12345) & 0x7fffffff;
      state[0] = val;
      *result = val;
    }
  else
    {
      int32_t *fptr = buf->fptr;
      int32_t *rptr = buf->rptr;
      int32_t *end_ptr = buf->end_ptr;
      int32_t val;

      val = *fptr += *rptr;
      /* Chucking least random bit.  */
      *result = (val >> 1) & 0x7fffffff;
      ++fptr;
      if (fptr >= end_ptr)
    {
      fptr = state;
      ++rptr;
    }
      else
    {
      ++rptr;
      if (rptr >= end_ptr)
        rptr = state;
    }
      buf->fptr = fptr;
      buf->rptr = rptr;
    }
  return 0;

 fail:
  __set_errno (EINVAL);
  return -1;
}


回答3:

You can browse the source code for different implementations of the C standard.

The question has been answered before, you might find what you're looking for at What common algorithms are used for C's rand()?

That answer provides code for glibc's implementation of rand()



回答4:

I guess, THIS is what you are looking for. It contains the detailed explanation of random function, and simple C program to understand the algo.

Edit:

You should check THIS as well. A possible duplicate.



回答5:

Well, I believe rand is from the C standard library, not the C++ standard library. There is no one implementation of either library, there are several.

You could go somewhere like this page to view the source code for glibc, the c library used on most Linux distributions. For glibc you'd find it in source files under stdlib such as rand.c and random.c.

A different implementation, such as uClibc might be easier to read. Try here under the libc/stdlib folder.



回答6:

The simplest reasonably good pseudo-random number generators are Linear Congruential Generators (LCGs). These are iterations of a formula such as

X_{n+1} = (a * X_n  +  c) modulo m

The constants a, c, and m are chosen to given unpredictable sequences. X_0 is the random seed value. Many other algorithms exists, but this is probably enough to get you going.

Really good pseudo-random number generators are more complex, such as the Mersenne Twister.



标签: c++ random