A
Acacia
How would you generate a random number in C++?
Acacia said:How would you generate a random number in C++?
How would you generate a random number in C++?
#include said:#include <cstdlib>
int main()
{
std::srand( 222 );
std::cout << std::rand() << "\n";
}
VC++ 1.5? You've got to be kidding. That is ancient history. If you wantAcacia said:Could you please give me code for a working program that I can compile in
either borland and/or msvc (v1.5) (note they are both old versions) that
will generate three random numbers, to be placed in three different integers
and then displayed to the screen. Thank you.
Acacia said:Upon compling (in MSVC) it returned the error:
Compiling...
c:\random.cpp
c:\random.cpp(2) : fatal error C1083: Cannot open include file: 'cstdlib.h':
No such file or directory
CL returned error code 2.
RANDOM.CPP - 1 error(s), 0 warning(s)
that was after placing a '.h' after iostream and cstdlib. Before doing this
returned this error:
Compiling...
c:\random.cpp
c:\random.cpp(1) : fatal error C1083: Cannot open include file: 'iostream':
No such file or directory
CL returned error code 2.
RANDOM.CPP - 1 error(s), 0 warning(s)
There are seven errors and one warning when an attempt at compiling is made
with the absence of the line '#include<cstdlib.h>'. These are:
Compiling...
c:\random.cpp
c:\random.cpp(6) : error C2653: 'std' : is not a class name
c:\random.cpp(6) : error C2065: 'srand' : undeclared identifier
c:\random.cpp(6) : error C2064: term does not evaluate to a function
c:\random.cpp(8) : error C2653: 'std' : is not a class name
c:\random.cpp(8) : error C2653: 'std' : is not a class name
c:\random.cpp(8) : error C2065: 'rand' : undeclared identifier
c:\random.cpp(8) : error C2064: term does not evaluate to a function
c:\random.cpp(9) : warning C4508: 'main' : function should return a value;
'void' return type assumed
CL returned error code 2.
RANDOM.CPP - 7 error(s), 1 warning(s)
Could you please give me code for a working program that I can compile in
either borland and/or msvc (v1.5) (note they are both old versions) that
will generate three random numbers, to be placed in three different integers
and then displayed to the screen. Thank you.
Acacia said:Upon compling (in MSVC) it returned the error:
Compiling...
c:\random.cpp
c:\random.cpp(2) : fatal error C1083: Cannot open include file: 'cstdlib.h':
No such file or directory
CL returned error code 2.
RANDOM.CPP - 1 error(s), 0 warning(s)
that was after placing a '.h' after iostream and cstdlib. Before doing this
returned this error:
Compiling...
c:\random.cpp
c:\random.cpp(1) : fatal error C1083: Cannot open include file: 'iostream':
No such file or directory
CL returned error code 2.
RANDOM.CPP - 1 error(s), 0 warning(s)
There are seven errors and one warning when an attempt at compiling is made
with the absence of the line '#include<cstdlib.h>'. These are:
Compiling...
c:\random.cpp
c:\random.cpp(6) : error C2653: 'std' : is not a class name
if the only header included was the non-standard said:c:\random.cpp(6) : error C2065: 'srand' : undeclared identifier
Yes said:c:\random.cpp(6) : error C2064: term does not evaluate to a function
c:\random.cpp(8) : error C2653: 'std' : is not a class name
c:\random.cpp(8) : error C2653: 'std' : is not a class name
c:\random.cpp(8) : error C2065: 'rand' : undeclared identifier
Also in said:c:\random.cpp(8) : error C2064: term does not evaluate to a function
c:\random.cpp(9) : warning C4508: 'main' : function should return a value;
'void' return type assumed
CL returned error code 2.
RANDOM.CPP - 7 error(s), 1 warning(s)
Could you please give me code for a working program that I can compile in
either borland and/or msvc (v1.5) (note they are both old versions) that
will generate three random numbers, to be placed in three different integers
and then displayed to the screen. Thank you.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.