10

I've tried for hours to find the implementation of rand() function used in gcc... It would be much appreciated if someone could reference me to the file containing it's implementation or website with the implementation.

By the way, which directory (I'm using Ubuntu if that matters) contains the c standard library implementations for the gcc compiler?

Sason
  • 139
  • 1
  • 3
  • 6
  • 3
    GCC doesn't implement the rand() at all, its a compiler, all functions come from libaries, in this case from the glibc. – theomega Oct 14 '10 at 12:23

3 Answers3

15

rand consists of a call to a function __random, which mostly just calls another function called __random_r in random_r.c.

Note that the function names above are hyperlinks to the glibc source repository, at version 2.28.

The glibc random library supports two kinds of generator: a simple linear congruential one, and a more sophisticated linear feedback shift register one. It is possible to construct instances of either, but the default global generator, used when you call rand, uses the linear feedback shift register generator (see the definition of unsafe_state.rand_type).

Tom Anderson
  • 46,189
  • 17
  • 92
  • 133
9

You will find C library implementation used by GCC in the GNU GLIBC project.

You can download it sources and you should find rand() implementation. Sources with function definitions are usually not installed on a Linux distribution. Only the header files which I guess you already know are usually stored in /usr/include directory.

If you are familiar with GIT source code management, you can do:

$ git clone git://sourceware.org/git/glibc.git

To get GLIBC source code.

Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
0

The files are available via FTP. I found that there is more to rand() used in stdlib, which is from [glibc][2]. From the 2.32 version (glibc-2.32.tar.gz) obtained from here, the stdlib folder contains a random.c file which explains that a simple linear congruential algorithm is used. The folder also has rand.c and rand_r.c which can show you more of the source code. stdlib.h contained in the same folder will show you the values used for macros like RAND_MAX.

/* An improved random number generation package. In addition to the standard rand()/srand() like interface, this package also has a special state info interface. The initstate() routine is called with a seed, an array of bytes, and a count of how many bytes are being passed in; this array is then initialized to contain information for random number generation with that much state information. Good sizes for the amount of state information are 32, 64, 128, and 256 bytes. The state can be switched by calling the setstate() function with the same array as was initialized with initstate(). By default, the package runs with 128 bytes of state
information and generates far better random numbers than a linear
congruential generator. If the amount of state information is less than 32 bytes, a simple linear congruential R.N.G. is used. Internally, the state information is treated as an array of longs; the zeroth element of the array is the type of R.N.G. being used (small integer); the remainder of the array is the state information for the R.N.G. Thus, 32 bytes of state information will give 7 longs worth of state information, which will allow a degree seven polynomial. (Note: The zeroth word of state
information also has some other information stored in it; see setstate for details). The random number generation technique is a linear feedback shift register approach, employing trinomials (since there are fewer terms to sum up that way). In this approach, the least significant bit of all the numbers in the state table will act as a linear feedback shift register, and will have period 2^deg - 1 (where deg is the degree of the polynomial being used, assuming that the polynomial is irreducible and primitive). The higher order bits will have longer periods, since their values are also influenced by pseudo-random carries out of the lower bits. The
total period of the generator is approximately deg*(2deg - 1); thus doubling the amount of state information has a vast influence on the
period of the generator. Note: The deg*(2
deg - 1) is an approximation only good for large deg, when the period of the shift register is the dominant factor. With deg equal to seven, the period is actually much longer than the 7*(2**7 - 1) predicted by this formula. */

Nav
  • 19,885
  • 27
  • 92
  • 135