Question about rand and srand

This is a discussion on Question about rand and srand within the C++ Programming forums, part of the General Programming Boards category; Greetings! I am new to C++ and I am currently reading jumping into C++ featured on this website. I am ...

  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,266
    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%.

  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, 11:58 AM
  5. Stupid question about rand() and srand()
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 12-07-2001, 01:37 PM

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