Generate a random number

G

Gianni Mariani

Acacia said:
How would you generate a random number in C++?

The usual way: srand needs to be seeded with a random number and
psuedo random numbers come out of rand().

#include <iostream>
#include <cstdlib>

int main()
{
std::srand( 222 );

std::cout << std::rand() << "\n";
}


If however you want cryptogrphically secure random number, you'll need
to do some more work that is platform dependant.
 
A

Acacia

#include said:
#include <cstdlib>

int main()
{
std::srand( 222 );

std::cout << std::rand() << "\n";
}

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.
 
R

Ron Natalie

Acacia 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.
VC++ 1.5? You've got to be kidding. That is ancient history. If you want
to find out what might work in something that doesn't even know there is
such a thing as standard C++, you should probably go to a group with
microsoft in it's name, or at least read whatever passes for documetnation
with those compilers. rand() or random() are the common names for those
functions.
 
B

Bob Jacobs

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.

Unfortunately you're using a very old compiler, so you'll need to use
pre-standard headers. Something along the following lines should work (but I
don't have VC++ 1.5 to test it with - if not, consult the VC++ help files
and look up rand and srand).

#include <iostream.h>
#include <stdlib.h>
#include <time.h>

int main()
{
srand((unsigned) time(NULL));
int a = rand();
int b = rand();
int c = rand();
cout << a << " " << b << " " << c << endl;
return 0;
}

As this group discusses standard C++, posts that are specific to VC++ are
considered off-topic so you'll need to ask further questions in a newsgroup
that discusses VC++, or perhaps better, search Google for C++ forums and try
some of the sites listed - some of those may be more tolerant of questions
for such an old compiler.

Also, be aware that C++ has changed somewhat since VC++ 1.5 was current, so
you'd be well advised to junk it in favour of a more up to date compiler
and, presumably, an up to date book. Free compilers are available for some
platforms. The FAQ may prove useful to you here:

http://www.parashift.com/c++-faq-lite/

HTH
 
K

Kevin Goodsell

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

There is no such standard header. No surprise here.
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:

Then you introduced errors where there were none. The code above has the
correct headers specified.
Compiling...
c:\random.cpp
c:\random.cpp(1) : fatal error C1083: Cannot open include file: 'iostream':
No such file or directory

Then the compiler is either broken, or very, very old.
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

No, it's not. It's a namespace name. But the compiler wouldn't see that
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

This is an error in the compiler. Main implicitly returns 0 if it
reaches the end without finding a return statement. Making main return
void makes it illegal.
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.

Since those compilers are too old to support standard C++, any such code
would not really be topical here. But here's an adaptation of the code
above for old, pre-standard compilers:

#include <iostream.h>
#include <stdlib.h>

int main()
{
srand( 222 );

cout << rand() << "\n";
return 0;
}

-Kevin
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top