/* FILE: randPermutGame.cpp last change: 2-Jul-2013 author: Romeo Rizzi * randomly generates k permutations of 1,2, ..., n. * Usage syntax: * > randPermutGame n k seed */ #include #include #include #include using namespace std; const int MAX_N = 1000000; int n; const int MAX_K = 100; int k; int perm_n[MAX_N+1]; // elementi numerati da 1 a n inline void swap(int &a, int &b) { int tmp = a; a = b; b = tmp; } void generaPerm_random_uniform(int *perm, int n) { for(int i=n; 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 + (int) ( (max-min +1) * (double( rand()-0.000000000001 ) / RAND_MAX ) ); } int main(int argc, char** argv) { n = atoi(argv[1]); k = atoi(argv[2]); srand(time(NULL)); if(argc > 3) srand( atoi(argv[3]) ); cout << n << " " << k << endl; for(int j = 1; j <= k; j++) { for(int i = 1; i <= n; i++) perm_n[i] = i; generaPerm_random_uniform(perm_n, n); for(int i = 1; i <= n; i++) cout << perm_n[i] << " "; cout << endl; } return 0; }