Thread: Rand() Function

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    Buea Cameroon
    Posts
    64

    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. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    66
    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.

  4. #4
    Registered User
    Join Date
    Feb 2013
    Location
    Buea Cameroon
    Posts
    64
    I know how to use the random function i just wanted to know how the numbers are been Generated

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Feb 2013
    Location
    Buea Cameroon
    Posts
    64
    Quote Originally Posted by laserlight View Post
    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?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jun 2013
    Posts
    66
    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.

  9. #9
    Registered User
    Join Date
    Jul 2012
    Posts
    51
    Quote Originally Posted by acho.arnold View Post
    ...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'.

  10. #10
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by sonjared View Post

    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-14-2011, 08:08 AM
  2. Rand() function...
    By Ash1981 in forum C Programming
    Replies: 7
    Last Post: 01-26-2006, 09:04 AM
  3. rand() function?
    By s_ny33 in forum C++ Programming
    Replies: 2
    Last Post: 03-08-2005, 07:58 PM
  4. I need help using the rand() function
    By incognito in forum C++ Programming
    Replies: 14
    Last Post: 06-02-2002, 02:48 PM
  5. rand function
    By BOBBY HILL in forum C Programming
    Replies: 1
    Last Post: 05-03-2002, 10:15 AM