kuboon
3/29/2019 - 5:10 AM

perm_rand.c

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

#define N 16
#define M 16
#define NN 2


unsigned char x0[N]={0};
unsigned char x1[N]={0};
unsigned char x2[N]={0};

 void rp(unsigned char* a) {
        int i, j, x;
        for (i = 0; i < N; i++) {
            a[i] = i;
        }
        for (i = 0; i < N - 2; i++) {
            // rand from i+1 to N-1
            j = (rand() % (N - 1 - i)) + i + 1;

            // swap a[i] and a[j]
            x = a[j];
            a[j] = a[i];
            a[i] = x;
        }
        if (a[N - 1] == N - 1) {
            a[N - 1] = a[N - 2];
            a[N - 2] = N - 1;
        }


    }



void data(fp){
  unsigned long long int i,j=0,k=0;
  unsigned char a[N]={1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0};
  unsigned short aa=0;
  
  unsigned char z[N];
  unsigned char w[N];
  unsigned char ww[N]={0};
  
  for(i=0;i<N;i++)
    x2[x0[i]]=i;
  
  while(j<137438953){
    
    for(i=0;i<N;i++)
      ww[i]=a[x1[i]];
    for(i=0;i<N;i++)
	a[i]^=ww[i];
    
    for(cnt=0;cnt<NN;cnt++){
      
      for(i=cnt*8;i<8*cnt+8;i++){
	aa=aa<<1;
	aa^=a[i];
      }
    }

	  fwrite(&aa,NN,1,fp);
    
    
    for(i=0;i<N;i++)
      w[i]=x0[x1[x2[i]]];
    
    for(i=0;i<N;i++){
      x1[i]=w[i];
    }
    
    j++;
  }
}



int main(){
  FILE *fp
  time_t t;
  srand(clock()+time(&t));

  rp(x0);
  rp(x1);
  
  fp = fopen("rand.dat","wb");
  data(fp);
  fclose(fp);

  return 0;
}