473,503 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generate a random number

How would you generate a random number in C++?
Jul 19 '05 #1
6 20691
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
"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
> #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

"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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2136
by: vishal | last post by:
hi i want to generate a string which contains 5 characters and all 5 characters are randomly generated means it is not fixed that which string i will get after i execute function. so tell me...
3
6229
by: vishal | last post by:
i want to generate a random number of a fixed length so how can i do this ?? i know some function which returns a single random character at a time but is there any built-in function which...
12
4991
by: Sweety | last post by:
plz reply, thanks in advance. bye
4
10537
by: fatimahtaher | last post by:
Hi, I am supposed to create a program that generates a random number and then asks the user to guess the number (1-100). The program tells the user if he guessed too high or too low. If he...
19
13643
by: Sanchit | last post by:
I want to generate a randowm number between two numbers x and y i.e int randomNoBetween(int x, int y); Plz help Is there any such function??
2
4052
by: Man4ish | last post by:
How to generate random number in normal distribution without using any external library. I found few methods implementing it. But all use GSL or boost. I don't want to use any third party library....
0
7207
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7291
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7012
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5598
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
1522
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
402
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.