// CSCI 2720 Data Structures Spring 2004 // randdemo.cc // demonstrate srand( ) and rand( ) library functions #include using namespace std; int main(int argc, char* argv[]){ // argc and argv[] allow main( ) // to read command line arguments cout << "This is the output from program " << argv[0] << endl; // above, argv[0] is the command, i.e., the name of the executable if (argc < 2 ) // case of no second argument srand(0); // default value of "seed" is set to 0 else srand( atoi( argv[1])); // second argument converted from string // (alphabetic) to integer, then used as seed for (int i = 0; i < 20; i++) // 20 numbers are to be generated; cout << rand( ) << endl; // the output of rand( ) is a 16 bit // integer >= 0. return 0; } // Note that the seed was only set once, by the call to srand( ). // Subsequently, calls to rand( ) produced a sequence of "random" // numbers. Actually they are pseudo-random. You can generate // exactly the same sequence later by setting the seed to the same // value. Normally you won't want to do that unless you need to // compare two different algorithms on exactly the same input // data or sequence of operations.