Hey please i wish to know how theFunctions 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 declarationCode://rand() and srand()
Hey please i wish to know how theFunctions 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 declarationCode://rand() and srand()
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
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:
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.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 ); }
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
The C standard document offers a minimal portable definition:Does it rely mean that there is no standard definition of the rand() Function?
But it's correct to say that there is no standard definition that you can rely on for any given compiler.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; }
Or even better Eternally Confuzzled - Random Numbers Tutorial