how to write a perfect shuffle method in java

Solutions on MaxInterview for how to write a perfect shuffle method in java by the best coders in the world

showing results for - "how to write a perfect shuffle method in java"
Alexander
26 Sep 2016
1public static int[] RandomizeArray(int a, int b){
2		Random rgen = new Random();  // Random number generator		
3		int size = b-a+1;
4		int[] array = new int[size];
5 
6		for(int i=0; i< size; i++){
7			array[i] = a+i;
8		}
9 
10		for (int i=0; i<array.length; i++) {
11		    int randomPosition = rgen.nextInt(array.length);
12		    int temp = array[i];
13		    array[i] = array[randomPosition];
14		    array[randomPosition] = temp;
15		}
16 
17		for(int s: array)
18			System.out.println(s);
19 
20		return array;
21	}
Alessia
22 Jul 2018
1public static int[] RandomizeArray(int[] array){
2		Random rgen = new Random();  // Random number generator			
3 
4		for (int i=0; i<array.length; i++) {
5		    int randomPosition = rgen.nextInt(array.length);
6		    int temp = array[i];
7		    array[i] = array[randomPosition];
8		    array[randomPosition] = temp;
9		}
10 
11		return array;
12	}