dimanche 22 mars 2015

How to get the sequence of numbers back after random number generation

Am in the middle of an encryption algorithm, I have written the code to randomly shuffle a series of numbers in java. Here is the code:



public class permute6 {
public static void main(String args[])
{
Random rand = new Random();
int arr[] = {10,20,30,40,50,60,70,80};// Original Array

System.out.println("Original array is" );
for(int k = 0; k<arr.length;k++ )
System.out.print(arr[k] + "\t" );
System.out.println(" ");

int max = arr.length - 1;
int min = 0;
int rnum;
int larr = arr.length - 1;
int[] parr = new int[arr.length];// to store the permuted array
int flag = 0;// 'flag' to flag the recurring number
int plen = parr.length - 1;
for(int i = 0 ; i < arr.length ; i++)
{
rnum = (rand.nextInt((max-min)+1) + min);// roll for the
random number
parr[plen] = arr[rnum];
arr[rnum] = arr[larr];

larr--;// to reduce the size of the original array
plen--;// to make the parr to act like a stack
max--;
}

System.out.println("Permuted array is" );
for(int k = 0; k<arr.length;k++ )
System.out.print(parr[k] + "\t" );

System.out.println();
}
}


If the initial array is {10,20,30,40,50,60,70,80}


then one of the shuffled result is


50 20 30 10 60 80 70 40


Now if I transmit this array, then how to get back the original sequence at the destination side? What can be done to keep track of the sequence? What is missing here....Any help is appreciated!!


Aucun commentaire:

Enregistrer un commentaire