perfect shuffle java code example
Example 1: how to write a perfect shuffle method in java
public static int[] RandomizeArray(int[] array){
Random rgen = new Random();
for (int i=0; i<array.length; i++) {
int randomPosition = rgen.nextInt(array.length);
int temp = array[i];
array[i] = array[randomPosition];
array[randomPosition] = temp;
}
return array;
}
Example 2: how to write a perfect shuffle method in java
public static int[] RandomizeArray(int a, int b){
Random rgen = new Random();
int size = b-a+1;
int[] array = new int[size];
for(int i=0; i< size; i++){
array[i] = a+i;
}
for (int i=0; i<array.length; i++) {
int randomPosition = rgen.nextInt(array.length);
int temp = array[i];
array[i] = array[randomPosition];
array[randomPosition] = temp;
}
for(int s: array)
System.out.println(s);
return array;
}