#include /* Generate some random data for a CSE2304 prac problem */ long rand(int lo, int hi) /* return a pseudo random number in [lo,hi] */ { int range; range = hi-lo+1; return lo + random() % range; } gen(int N, int nPerLine, int seed, int lo, int hi) /* Print N random numbers from [lo,hi], nPerLine of them to the line. */ { int i; if(seed > 0) srandom( seed ); /* to seed it or not? */ for( i = 1; i <= N; i++) { printf("%i", rand(lo, hi)); if(i % nPerLine == 0 || i == N) printf("\n"); else printf(" "); } }/* gen */ pairs(int P, int lo, int hi) { int i, p, q, temp; for( i = 1; i <= P; i++) { p = rand(lo, hi); q = rand(lo, hi); if( q < p ) /* swap */ { temp = p; p = q; q = temp; } printf("%i %i\n", p, q); } } /* ------------------------------------------------------------------------- */ main(int argc, char *argv[]) { int N, M, seed, lo, rng; if (argc < 4) /* # of command line arguments */ { printf("error, usage: prog N M seed\n"); exit(1); } sscanf(argv[1], "%i", &N); sscanf(argv[2], "%i", &M); sscanf(argv[3], "%i", &seed); /* seed for random */ if( seed <= 0 ) seed = 1231; /* printf("N = %i, M = %i, seed = %i\n", N, M, seed); */ lo = -5; rng = 10; for( ; rng < 10000000; ) /* mttthhh */ { if( 3*rng >= N ) break; lo = lo*10; rng = rng*10; } gen(N, 10, seed, lo, lo+rng); printf("\n"); pairs(M, 0, N-1); } /* L.Allison, CSSE, Monash, .au, January 2004 */