/* FILE: randArrayInstance.cpp last change: 24-Jan-2013 author: Romeo Rizzi * This program generates a random instance for problem dynArray. * Usage syntax: * > randArrayInstance n m n_blocks seed * where A[1..n] will be the dynamic array, m the number of operations, alternated in n_blocks blocks of updates and queries. * Usage example: * > randArrayInstance 1000 1000 500 777 */ #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) * (double( rand()-0.000000000001 ) / RAND_MAX ) ); } int main(int argc, char** argv) { srand(time(NULL)); int n = atoi(argv[1]); int m = atoi(argv[2]); int n_blocks = atoi(argv[3]); if(argc > 4) srand( atoi(argv[4]) ); cout << n << " " << m << endl; int b_size = m/n_blocks; int pos_inblock, made = 0; for(int i = 1; i <= n_blocks; i++) { pos_inblock = 1; while( pos_inblock <= b_size/2 ) { if( RandNumber(0,1) == 1 ) cout << "-"; cout << RandNumber(1,9) << " " << RandNumber(1,n) << endl; made++; pos_inblock++; } while( pos_inblock++ <= b_size ) { int a = 1; int b = n; if( RandNumber(0,6) > 0 ) a = RandNumber(1,b); if( RandNumber(0,6) > 0 ) b = RandNumber(a,b); if( RandNumber(0,6) == 0 ) b = a; cout << "0 " << a << " " << b << endl; made++; } } while( made < m ) { int a = 1; int b = n; if( RandNumber(0,6) > 0 ) a = RandNumber(1,b); if( RandNumber(0,6) > 0 ) b = RandNumber(a,b); if( RandNumber(0,6) == 0 ) b = a; cout << "0 " << a << " " << b << endl; made++; } return 0; }