Thread: Question about rand and srand

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    13

    Question about rand and srand

    Greetings!

    I am new to C++ and I am currently reading jumping into C++ featured on this website. I am reading about the rand and srand functions. The book claims you need a function prototype for these functions however when i input the prototypes my compiler gives me an error untill i comment out the prototypes. Is this expected and if so why? I have copied my code below to give an example with the offending code commented out.

    Thank you for your help!

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    
    int main()
    {
    
    //void srand(int x);//function prototype to generate random seed.
    //int rand; // function prototype for random number generation.
    
    int random_seed = time( NULL ); // set random seed based on unix clock
    srand(random_seed);
    cout << rand() << "\n";
    cout << rand() << "\n";
    cout << rand() << "\n";
    cout << rand() << "\n";
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The declarations (aka function prototypes) for rand() and srand() are in <cstdlib>.

    The declarations you have commented out are in conflict with the declarations in <cstdlib>, hence the compilation errors.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    13
    Quote Originally Posted by grumpy View Post
    The declarations (aka function prototypes) for rand() and srand() are in <cstdlib>.

    The declarations you have commented out are in conflict with the declarations in <cstdlib>, hence the compilation errors.
    grumpy,

    Thank you for your time. That makes perfect sense!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. srand and rand
    By pobri19 in forum C++ Programming
    Replies: 4
    Last Post: 10-12-2008, 07:09 AM
  2. srand() and rand()
    By Mr.Sellars in forum C++ Programming
    Replies: 3
    Last Post: 08-12-2007, 03:19 PM
  3. rand() and srand() question
    By the_winky_files in forum C Programming
    Replies: 4
    Last Post: 09-21-2005, 10:43 AM
  4. srand() or rand()???
    By bajanstar in forum C Programming
    Replies: 4
    Last Post: 03-04-2005, 12:58 PM
  5. Stupid question about rand() and srand()
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 12-07-2001, 02:37 PM