how to seed randomness in iostream

Started by
5 comments, last by Reckoner 16 years, 8 months ago
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.
DO NOT TIME TRAVEL! when you change your position in time, your position in space stays the same relative to the point of the big bang. meanwhile Earth is now somewhere very far away. IN OTHER WORDS: u arn't in Kansas anymore.
Advertisement
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.
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.)
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.
DO NOT TIME TRAVEL! when you change your position in time, your position in space stays the same relative to the point of the big bang. meanwhile Earth is now somewhere very far away. IN OTHER WORDS: u arn't in Kansas anymore.
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).

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"
DO NOT TIME TRAVEL! when you change your position in time, your position in space stays the same relative to the point of the big bang. meanwhile Earth is now somewhere very far away. IN OTHER WORDS: u arn't in Kansas anymore.
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.

This topic is closed to new replies.

Advertisement