Like Tree4Likes
  • 4 Post By grumpy

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

This is a discussion on I don't need to include <cstdlib> to call rand() function? within the C++ Programming forums, part of the General Programming Boards category; hey all, been a while (damn jobs) so im familiar with the rand() function, and how to get a random ...

  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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,656
    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,675
    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
    ZuK
    ZuK is offline
    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,675
    While what Zuk said is true,at the same link,at their own example,they do not use it .

  6. #6
    ZuK
    ZuK is offline
    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,675
    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,245
    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%.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    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.
    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

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    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, 07: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, 07:53 PM
  4. Rand() function
    By Da-Nuka in forum C++ Programming
    Replies: 10
    Last Post: 12-26-2004, 07:12 AM
  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