/* FILE: randGoods.cpp last change: 26-Feb-2013 author: Romeo Rizzi * This program generates a sequence of n random goods, where a good is a pair of naturals p <= MAX_P and v <= MAX_V * Usage syntax: * > randGoods n MAX_P MAX_V parity_Sum_p parity_Sum_v seed */ #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 acco unt. */ return min + (int) ( (max-min +1) * (double( rand()-0.000000000001 ) / RAND_MAX ) ); } int main(int argc, char** argv) { srand(time(NULL)); int n, MAX_P = 10, MAX_V = 10, parity_Sum_p = 0, parity_Sum_v = 0; n = atoi(argv[1]); if(argc > 2) MAX_P = atoi(argv[2]); if(argc > 3) MAX_V = atoi(argv[3]); if(argc > 4) parity_Sum_p = atoi(argv[4]); if(argc > 5) parity_Sum_v = atoi(argv[5]); if(argc > 6) srand( atoi(argv[6]) ); cout << n << " 2" << endl; // genero solo istanze con num figli f = 2 int p, v; for(int i = 1; i <= n; i++) { p = RandNumber(0, MAX_P); v = RandNumber(0, MAX_V); parity_Sum_p = (parity_Sum_p + p) %2; parity_Sum_v = (parity_Sum_v + v) %2; if( i == n ) { if( parity_Sum_p ) { p = p+1; if( p > MAX_P ) p = 1 - (MAX_P % 1); } if( parity_Sum_v ) { v = v+1; if( v > MAX_V ) v = 1 - (MAX_V % 1); } } cout << p << " " << v << endl; } return 0; }