using rand() function

I did a couple programs for my c++ class using rand() and by mistake I did not include <ctime>.
I got some points taken off and I know it should have included it.
The program did work though in my devc++ compiler, but it did not work on my professor compiler, I would like to know if there is any reason for this particularity of this function.
Thanks.
This is not particularity, this is very normal. Usually when you compile with different compilers or different versions of compilers you fall into such problems.

Yesterday I was compiling an OpenGL program with Visual Studio Compiler, which I have compiled already with MinGW. It took me more than 1 hours to retweak everything to fit both compilers. And it's not only include problems, it's, also, for example that Visual Studio Compiler's <cmath> doesn't have an std::round() function, so I had to write my own round function that doesn't conflict with MinGW but is accepted by both compilers and give correct results.

Another example, in Qt 4.7.4, the OpenGL widget included glu.h internally, and in Qt 4.8.0 it wasn't there, and the programmer has to include it manually. Everyone complained about this, and one sees always people in Qt Forums complaining that their program doesn't compile anymore... that's just normal!!!

Welcome to 7975649113151 compilers' world :-)
Last edited on
You are both wrong. std::rand function is a sttandard C/C++ function which is declared in <cstdlib> ( <stdlib.h> in C ). So there shall no any difference what compiler will compile your program with std::rand faunction.

So I think you should search the reason that you gets different result on different computers in your incorrect program.
<ctime> was probably needed because you use std::time to pass as seed to std::srand.
@Peter87
<ctime> was probably needed because you use std::time to pass as seed to std::srand


No, <ctime> is not needed. Otherwise the compiler will report an error that std::time is not found.
I think that the original problem has nothing common with <ctime>
closed account (1vRz3TCk)
mhceotto,

I did a couple programs for my c++ class using rand() and by mistake I did not include <ctime>.
I got some points taken off and I know it should have included it.
The program did work though in my devc++ compiler, but it did not work on my professor compiler, I would like to know if there is any reason for this particularity of this function.


Different compilers implement their libraries in different ways. Your compiler probably included the correct header as a result of including something else and the compiler used by your professor does not do this.

The thing to remember is if you use a function from a header, include the header.
Topic archived. No new replies allowed.