/* FILE: randConnect.cpp last change: 10-Feb-2015 author: Romeo Rizzi * This program generates an mxn chessboard with p pebbles randomly located on its cells. * Usage syntax: * > randConnect out_file m n p seed */ #include #include #include #include using namespace std; const int MAXM = 1000; const int MAXN = 1000; int B[MAXM][MAXN]; 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 m, n, p; m = atoi(argv[2]); n = atoi(argv[3]); p = atoi(argv[4]); if(argc > 5) srand( atoi(argv[5]) ); int put = 0; while(put < p) { int i = RandNumber(0, m-1); int j = RandNumber(0, n-1); if( !B[i][j] ) { B[i][j] = 1; put++; } } ofstream fout(argv[1]); fout << m << " " << n << " " << p << endl; for(int i = 0; i < m; i++) for(int j = 0; j < n; j++) if(B[i][j]) fout << i+1 << " " << j+1 << endl; fout.close(); return 0; }