// Generate random permutation of n integers. // After http://cap-lore.com/code/ocaml/misc.ml #include #define C const static char C * C a = "0123456789abcdef"; void rp(int n, char ans[n]){ char ra[n]; {int j=n; while(j--) ra[j]=j;} {int i=n; while(i--){ int f = random()%(i+1); ans[i] = *(a+ra[f]); ra[f] = ra[i];}}} typedef unsigned long int P; #include #include static P cn = 0, cx=0; static P sw[] = {0x0123456789ABCDEF, 0x021346578A9BCEDF, 0x041526378C9DAEBF, 0x08192A3B4C5D6E7F, 0x123C485A697BDE, 0X14283A596C7DBE, 0X24356879ACBD, 0X36587A9C, 0X3456789ABC, 0}; int main(){int w=100000000; while(w--) {char y[17]; char u[16]; rp(16, y); memcpy(u, y, 16); y[16]=0; P * c = sw; while(*c){P p = *(c++); while(p){int k = p&15, i = (p>>4)&15; if(u[i]>u[k]){char t = u[i]; u[i] = u[k]; u[k] = t;} p >>= 8;}} if(memcmp(u, a, 16)) {if(0) ++cn; else { {for(int j = 0; j<16; ++j) printf ("%c%c%c ", u[j], y[j], a[j]);} exit(printf("Foo\n"));}} if(!(cx++& ((1L<<20)-1)))printf("%s %ld %ld\n", y, cx>>20, cn);} return 0;}