/* FILE: randSeqB.cpp last change: 23-July-2012 author: Romeo Rizzi * This program generates a sequence of n random integers. * Usage syntax: * > randSeq out_file n B MIN_MAV MAX_VAL seed */ #include #include #include #include using namespace std; int RandNumber(int min, int max) { /* returns an integer in [min, max] * see Stroustrup "The c++ Programming Language" 3th edition pg. 685 * for comments on the following manipulation choice. * In particular, considerations on the bad quality of low bits come into account. */ return min + (int) ( (max-min +1) * (static_cast(std::rand()) / RAND_MAX ) ); } int main(int argc, char** argv) { srand(time(NULL)); int n, B = 100, MIN_VAL = -100, MAX_VAL = 90; n = atoi(argv[2]); if(argc > 3) B = atoi(argv[3]); if(argc > 4) MIN_VAL = atoi(argv[4]); if(argc > 5) MAX_VAL = atoi(argv[5]); if(argc > 6) srand( atoi(argv[6]) ); ofstream fout(argv[1]); fout << n << " " << B << endl; for(int i = 1; i <= n; i++) fout << RandNumber(MIN_VAL, MAX_VAL) << " "; fout << endl; fout.close(); return 0; }