By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,890 Members | 1,226 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,890 IT Pros & Developers. It's quick & easy.

Generate a random number

P: n/a
How would you generate a random number in C++?
Jul 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Acacia wrote:
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.

Jul 19 '05 #2

P: n/a
"Acacia" <ne*******@fatgerbil.co.uk> wrote in news:bidtsq$t2a$1$8300dec7
@news.demon.co.uk:
How would you generate a random number in C++?


Please visit Bob Jenkins' page http://www.burtleburtle.net/bob/
and see his work, esp. the "ISAAC" algorithm. It's also available as a C++
template class. It's said to be cryptographically secure, fast, and
unbiased.

regards
b
Jul 19 '05 #3

P: n/a
> #include <iostream>
#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.
Jul 19 '05 #4

P: n/a

"Acacia" <ne*******@fatgerbil.co.uk> wrote in message news:bi*******************@news.demon.co.uk...
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.
Jul 19 '05 #5

P: n/a

"Acacia" <ne*******@fatgerbil.co.uk> wrote in message
news:bi*******************@news.demon.co.uk...
#include <iostream>
#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.


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



Jul 19 '05 #6

P: n/a
Acacia wrote:
#include <iostream>
#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


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 <iostream.h>.
c:\random.cpp(6) : error C2065: 'srand' : undeclared identifier
Yes, because srand is declared in <cstdlib>.
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 <cstdlib>.
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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.