/* FILE: randGenInst.cpp last change: 29-Sep-2014 author: Romeo Rizzi * This program generates a sequence of n random integers. * Usage syntax: * > randGenInst out_file N L unfeas seed */ #include #include #include #include #include using namespace std; const int MAX_N = 1000; int N; const int MAX_L = 1000; int L; const int MAX_NL = 200000; int source[MAX_N][MAX_L], target[MAX_N][MAX_L]; int perm[MAX_N]; int flip_position[MAX_L]; inline void swap(int &a, int &b) { int tmp = a; a = b; b = tmp; } void randomPerm(int *perm, int n) { for(int i=0; i < n; i++) perm[i] = i; for(int i=n-1; i > 1; i--) swap( perm[i], perm[ (rand() % (i+1) ) ] ); } 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 + (rand() % (int)(max - min + 1)); } int main(int argc, char** argv) { srand(time(NULL)); N = atoi(argv[2]); assert( N <= MAX_N); L = atoi(argv[3]); assert( L <= MAX_L); assert( N*L <= MAX_NL); int unfeas = atoi(argv[4]); if(argc > 5) srand( atoi(argv[5]) ); for(int i = 0; i < N; i++) for(int j = 0; j < L; j++) source[i][j] = RandNumber(0, 1); ofstream fout(argv[1]); fout << N << " " << L << endl; for(int i = 0; i < N; i++) { for(int j = 0; j < L; j++) fout << source[i][j]; fout << " "; } fout << endl; randomPerm(perm, N); for(int j = 0; j < L; j++) flip_position[j] = RandNumber(0, 1); if(unfeas) { int i_error = RandNumber(0, N-1); int j_error = RandNumber(0, L-1); source[i_error][j_error] = 1 - source[i_error][j_error]; } for(int i = 0; i < N; i++) { for(int j = 0; j < L; j++) if( flip_position[j] ) fout << 1 - source[ perm[i] ][j]; else fout << source[ perm[i] ][j]; fout << " "; } fout << endl; fout.close(); return 0; }