# Rand() Function

This is a discussion on Rand() Function within the C Programming forums, part of the General Programming Boards category; Hey please i wish to know how the Code: //rand() and srand() Functions are declared I use linux and the ...

1. ## Rand() Function

Hey please i wish to know how the
Code:
`//rand() and srand()`
Functions are declared I use linux and the GCC compiler.I tried checking for the fuction in stdlib.h but i saw only the prototype i am looking for the full declaration

2. The declaration is all you'll find in header files, unless the function is also defined as a macro or template in C++. Some compilers will ship with source code for the standard libraries, and that is where you will find the definition. As an example, Visual Studio Express 2012 defines srand and rand as such:
Code:
```void __cdecl srand(unsigned int seed)
{
_getptd()->_holdrand = (unsigned long)seed;
}

int __cdecl rand(void)
{
_ptiddata ptd = _getptd();

return( ((ptd->_holdrand = ptd->_holdrand * 214013L + 2531011L) >> 16) & 0x7fff );
}```
Obviously not very helpful given that the opaque _ptiddata is used, but rand is typically defined as a linear congruential generator of one form or another.

3. I know how to use the random function i just wanted to know how the numbers are been Generated

4. In that case, you are thinking more about possible definitions of the function rather than (forward) declarations of it. sonjared's post #3 has a link to an article on linear congruential generators that can serve as a starting point to explore this topic.

5. Originally Posted by laserlight
In that case, you are thinking more about possible definitions of the function rather than (forward) declarations of it. sonjared's post #3 has a link to an article on linear congruential generators that can serve as a starting point to explore this topic.
Does it rely mean that there is no standard definition of the rand() Function?

6. Yes, there is no standard definition of rand. The standard specifies its declaration and outlines what it should do along with other constraints, but does not mandate any particular implementation.

7. Does it rely mean that there is no standard definition of the rand() Function?
The C standard document offers a minimal portable definition:
Code:
```static unsigned long int next = 1;

int rand(void) // RAND_MAX assumed to be 32767
{
next = next * 1103515245 + 12345;
return (unsigned int)(next/65536) % 32768;
}

void srand(unsigned int seed)
{
next = seed;
}```
But it's correct to say that there is no standard definition that you can rely on for any given compiler.

8. Originally Posted by acho.arnold
...I use linux and the GCC compiler.I tried checking for the fuction in stdlib.h...
GNU's random is found by looking at the glibc source code. It's located in 'stdlib/random.c'.

9. Originally Posted by sonjared

Obviously not very helpful given that the opaque _ptiddata is used, but rand is typically defined as a linear congruential generator of one form or another.
Or even better Eternally Confuzzled - Random Numbers Tutorial