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. #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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    21,414
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,414
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    50
    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
    107
    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
    In C++14 you just write "auto auto(auto auto) { auto; }".
    The compiler infers the rest from context.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-14-2011, 07:08 AM
  2. Rand() function...
    By Ash1981 in forum C Programming
    Replies: 7
    Last Post: 01-26-2006, 08:04 AM
  3. rand() function?
    By s_ny33 in forum C++ Programming
    Replies: 2
    Last Post: 03-08-2005, 06: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21