How would you generate a random number in C++?
Generate a random number
Collapse
This topic is closed.
X
X
-
Acacia -
Gianni Mariani
Re: Generate a random number
Acacia wrote:[color=blue]
> How would you generate a random number in C++?
>
>[/color]
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 cryptogrphicall y secure random number, you'll need
to do some more work that is platform dependant.
-
bartek d
Re: Generate a random number
"Acacia" <newsgroup@fatg erbil.co.uk> wrote in news:bidtsq$t2a $1$8300dec7
@news.demon.co. uk:
[color=blue]
> How would you generate a random number in C++?[/color]
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 cryptographical ly secure, fast, and
unbiased.
regards
b
Comment
-
Acacia
Re: Generate a random number
> #include <iostream>[color=blue]
> #include <cstdlib>
>
> int main()
> {
> std::srand( 222 );
>
> std::cout << std::rand() << "\n";
> }[/color]
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<cstdl ib.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.
Comment
-
Ron Natalie
Re: Generate a random number
"Acacia" <newsgroup@fatg erbil.co.uk> wrote in message news:bige55$3bc $1$830fa7a5@new s.demon.co.uk.. .
[color=blue]
> 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.
>[/color]
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.
Comment
-
Bob Jacobs
[OT] Re: Generate a random number
"Acacia" <newsgroup@fatg erbil.co.uk> wrote in message
news:bige55$3bc $1$830fa7a5@new s.demon.co.uk.. .[color=blue][color=green]
> > #include <iostream>
> > #include <cstdlib>
> >
> > int main()
> > {
> > std::srand( 222 );
> >
> > std::cout << std::rand() << "\n";
> > }[/color]
>
> Upon compling (in MSVC) it returned the error:
>
> Compiling...
> c:\random.cpp
> c:\random.cpp(2 ) : fatal error C1083: Cannot open include file:[/color]
'cstdlib.h':[color=blue]
> 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[/color]
this[color=blue]
> returned this error:
>
> Compiling...
> c:\random.cpp
> c:\random.cpp(1 ) : fatal error C1083: Cannot open include file:[/color]
'iostream':[color=blue]
> 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[/color]
made[color=blue]
> with the absence of the line '#include<cstdl ib.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[/color]
integers[color=blue]
> and then displayed to the screen. Thank you.[/color]
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:
HTH
Comment
-
Kevin Goodsell
Re: Generate a random number
Acacia wrote:
[color=blue][color=green]
>>#include <iostream>
>>#include <cstdlib>
>>
>>int main()
>>{
>> std::srand( 222 );
>>
>> std::cout << std::rand() << "\n";
>>}[/color]
>
>
> 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
>[/color]
There is no such standard header. No surprise here.
[color=blue]
> 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:[/color]
Then you introduced errors where there were none. The code above has the
correct headers specified.
[color=blue]
>
> Compiling...
> c:\random.cpp
> c:\random.cpp(1 ) : fatal error C1083: Cannot open include file: 'iostream':
> No such file or directory[/color]
Then the compiler is either broken, or very, very old.
[color=blue]
>
> 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<cstdl ib.h>'. These are:
>
> Compiling...
> c:\random.cpp
> c:\random.cpp(6 ) : error C2653: 'std' : is not a class name[/color]
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>.
[color=blue]
> c:\random.cpp(6 ) : error C2065: 'srand' : undeclared identifier[/color]
Yes, because srand is declared in <cstdlib>.
[color=blue]
> 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[/color]
Also in <cstdlib>.
[color=blue]
> 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[/color]
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.
[color=blue]
> 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.[/color]
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.
Comment
Comment