tags:

views:

75

answers:

2

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?

+7  A: 

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
Or, jump right into: http://sourceware.org/git/?p=glibc.git;a=tree
progo
+5  A: 

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 cross-referenced source of glibc 2.9.

From the comments, it looks like it was written by McGrath rather than Drepper, and it's a linear congruential generator.

Tom Anderson
It looks like it is somehow selectable between a linear congruential generator and a "fancier"(sic) algorythm.
ninjalj