Sign in to follow this  
GROUDON

how to seed randomness in iostream

Recommended Posts

GROUDON    122
I was wondering... What functions in the libraries <cstdio> <cstdlib> <iostream> and <time.h> seed for randomness and produce a random number. I don't know even if there is an iostream alternative to <time.h> or if that is what i use. Can someone show me a short program that would set up the libraries, seed for randomness, produce a random number from 1-20 (maybe by adding 1 to the random number) then displaying it on the screen and using system("PAUSE") so i can see it. here is what i can do so far
//showrandno.cpp
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <time.h>

using namespace std;

int main(void)
{
   int x;
   //seed a random number function
   //produce a random number function smaller than 20 then add 1
   //      and assign it to x
   cout << "the random number is: " << x << endl;
   system("PAUSE");
   return 0;
}
tell me if i got any of it wrong and/or fill in the blanks.

Share this post


Link to post
Share on other sites
ToohrVyk    1595
iostream has nothing to do with random numbers. It's the header in the C++ Standard Library which is responsible for the main input and output streams (mainly std::cout, std::cin and their ilk).

time.h is a C header. The C++ equivalent would be ctime.

cstdio are the C input/output functions. They are usually not used or included at all, and iostream is used instead.

The important functions are srand and rand, from cstdlib. Google these.

Share this post


Link to post
Share on other sites
jyk    2094
For simple random number generation in C/C++, there are really only two functions you need: rand() and srand(). (Not going to write up an example at the moment, but you should be able to find plenty of examples and info online.)

Share this post


Link to post
Share on other sites
GROUDON    122
Quote:
Original post by jyk
For simple random number generation in C/C++, there are really only two functions you need: rand() and srand(). (Not going to write up an example at the moment, but you should be able to find plenty of examples and info online.)


Thanx, i got it working. it's just like randomness in the C libraries. (What A Surprise!) here's my code that works.
//showrandno.cpp
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <ctime>

using namespace std;

int main(void)
{
int x;
srand((unsigned)time(NULL));
x=(rand()%20)+1;
cout << "the random number is: " << x << endl;
system("PAUSE");
return 0;
}


Now i'm one step closer to making a game. the next step: learning how to use class(es).

EDIT: Correction. This is actually just like using psuedorandomness in the C libraries.

Share this post


Link to post
Share on other sites
ToohrVyk    1595
Correct version:

//showrandno.cpp
#include <cstdlib>
#include <iostream>
#include <ctime>

int main()
{
std::srand(
static_cast<unsigned>(std::time(0)));

int x = std::rand() % 20 + 1;
std::cout << "the random number is: " << x << "\n";
}


Notice that:
  • cstdio is gone (it wasn't used).
  • x is now both defined and initialized at the same time, as is the approach in C.
  • static_cast<unsigned> is used instead of the C-style cast (unsigned)
  • The C++ 0 literal has replaced the C NULL constant.
  • The return 0 is also gone, since it's implied by the main in C++ .
  • Speaking of which, the C-style void argument list of main has been converted to C++ style.
  • Since std::endl was neither useful nor defined (remember, it's defined in ostream, so including iostream only won't include std::endl on all compilers), it was replaced with a plain old newline: "\n".
  • I also dropped the using namespace std; which saved no typing at all.
  • I removed the unnecessary parentheses around the remainder operation, which has a higher priority than addition anyway (it's the same priority as * and /).
  • And, while we're at it, I dropped the system("pause") call, which was non-portable and generally considered to be inelegant—if you want your window to stick around, check your IDE setting about doing that (in Visual Studio 2005 Express Edition, it's Ctrl+F5).

Share this post


Link to post
Share on other sites
GROUDON    122
problems:
1: i use MVS .NET 2003. YOU use MVS .NET 2005 Express.
2: when i used your code then pressed ctrl+F5 it ran perfectly. BUT when i ran it from windows explorer, it doesn't pause!
3: now i have no way to pause in the middle of the program w/o having to use std::cin. Or it'll be "non-portable"

Share this post


Link to post
Share on other sites
Reckoner    206
Quote:
Original post by GROUDON
i use MVS .NET 2003. YOU use MVS .NET 2005 Express.


This shouldn't matter. They both automatically pause programs when they are run from the IDE. MSVC++ 2005 Express is, however, offered free for download if you want it.

Quote:
BUT when i ran it from windows explorer, it doesn't pause!


This is the desirable behavior. Console programs should be intended to run from the command line, where they should just do their thing and then exit. I know I would be annoyed if all of the console programs that I use (version control software, command-line compilation tools, etc) paused after every usage and required me to hit a key to continue.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this