// Program to generate a stream of random numbers. // // output is integers, one on each line // #include #include #include #include void getargs(int, char*[]); // These are global because I am lazy int iterations; float probability; int maxlength; int main(int argc, char* argv[]) { int i; getargs(argc, argv); srand(time(NULL)); // nice random seed for (i = 0; i < iterations; i++) { if ( (float) rand() / RAND_MAX <= probability) { cout << (int) ((float) rand() / RAND_MAX * (float) maxlength) + 1 << endl; // must add 1 to get the desired range. the cast truncates the fraction } else { cout << "0" << endl; } } } // ------------------------------------------------------------------------- void getargs(int argc, char* argv[]) { if (argc == 4) { iterations = atoi(argv[1]); probability = atof(argv[2]); maxlength = atoi(argv[3]); } else { cout << "USAGE:" << endl; cout << " jobgen [iterations] [job probability] [max length]" << endl; exit(1); } }