/* FILE: randWood.cpp last change: 30-Sep-2014 author: Romeo Rizzi * This program generates a random wood. * Usage syntax: * > randWood out_file n 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) * (double(rand()) / RAND_MAX) ); } int main(int argc, char** argv) { srand(time(NULL)); int n, MIN_VAL = 1, MAX_VAL = 40; n = atoi(argv[2]); if(argc > 3) MAX_VAL = atoi(argv[3]); if(argc > 4) srand( atoi(argv[4]) ); int tmp = n; if (tmp > 50) tmp = 50; ofstream fout(argv[1]); fout << n << endl; for(int i = 1; i <= n; i++) if(RandNumber(0, tmp) < tmp-2) fout << "1 "; else fout << RandNumber(MIN_VAL, MAX_VAL) << " "; fout << endl; fout.close(); return 0; }