Thread: I don't need to include <cstdlib> to call rand() function?

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    21

    I don't need to include <cstdlib> to call rand() function?

    hey all, been a while (damn jobs)

    so im familiar with the rand() function, and how to get a random number within a certain range, but as i was flipping through one of my books i noticed its telling me that i need to include <cstdlib> in order to call the rand() function.. however I ran the following code without it and got the same (random) results...

    im just curious about how this is happening. Is it simply good practice to include <cstdlib> or was there some kind of C++ update that allows the rand() function to be called without it? not of uttermost importance, just curious

    Code:
    #include<iostream>
    #include <ctime>
    
    
    using namespace std;
    
    
    int main()
    {
    
     srand(time(0));
     
     for (int i = 0; i < 15; i++)
          {
           int randomNumber= (rand() % 6) + 1;
     
           cout<< randomNumber << endl;
           }
     system("pause");
    }
    this code runs fine for me and still generates random numbers from 1 - 6...

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It is not simply good practice. The code will not compile for me.
    main.cpp:11: error: 'srand' was not declared in this scope
    main.cpp:15: error: 'rand' was not declared in this scope
    main.cpp:19: error: 'system' was not declared in this scope
    Process terminated with status 1 (0 minutes, 0 seconds)
    3 errors, 0 warnings
    What we can learn from this is that you always need to include the headers so that the types and functions you want to use are made ready. You are simply lucky that your implementation of the libraries include at some point the missing headers in your source code for you. You cannot depend on this, by way of my example.

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Well the code runs fine at me too.However i checked the ref and i saw that <cstdlib> is needed for RAND_MAX,not for rand().Check for yourselves rand - C++ Reference

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by std10093 View Post
    Check for yourselves rand - C++ Reference
    What are you saying ?. This link clearly tells that <cstdlib> has to be included for int rand( void ). Look at the top right.

    Kurt

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    While what Zuk said is true,at the same link,at their own example,they do not use it .

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    That's because this example is C-code and they do #include <stdlib.h>

    Kurt

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Having C code in a C++ site <--Genius So tell us Zuk,why did not the compiler complained?Neither to me or to Mike? I can not figure out

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    In C, rand() does not strictly require <stdlib.h> to be #include'd, since the compiler assumes an int return type and int arguments by default. This is, however, considered very poor technique.

    In C++, the using rand() without the appropriate header file is illegal. In practice, however, it depends on the implementation, since other header files sometimes #include <cstdlib> or <stdlib.h>. The problem that brings is that the C++ standard does not require standard headers to #include each other, so code which relies on it can break when taken to other compilers (or when a compiler/library is upgraded and the code is recompiled).
    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.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by std10093
    Having C code in a C++ site <--Genius
    <stdlib.h> is in the C++ standard library too, but with small differences from <cstdlib>, and is deprecated.
    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

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by laserlight View Post
    <stdlib.h> is in the C++ standard library too, but with small differences from <cstdlib>, and is deprecated.
    As <stdio.h> but i do not think that C++ developers use printf instead of cout.

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 thunderzone in forum C Programming
    Replies: 10
    Last Post: 05-02-2010, 07:09 AM
  3. Is there any function better than rand()
    By ubern00ber in forum C++ Programming
    Replies: 21
    Last Post: 03-09-2006, 08:53 PM
  4. Rand() function
    By Da-Nuka in forum C++ Programming
    Replies: 10
    Last Post: 12-26-2004, 08:12 AM
  5. rand function
    By BOBBY HILL in forum C Programming
    Replies: 1
    Last Post: 05-03-2002, 10:15 AM