user412308 - 23 days ago 8
Java Question

Random Shuffling an array of integers in Java

This is my first time with arrays.
I should prompt the user to enter 5 array values and then display them in random order.
I am quite confused, since it's my first time doing this.
Anyway, my code is here.

``````import java.util.*;
public class Test {
public static void main(String[] args) {
int myArray[] = new int[5];
Scanner input = new Scanner(System.in);
for (int i = 0; i < myArray.length - 1; i--) {
int j = (int) (Math.random() * (i + 1));
myArray[i] = input.nextInt();

System.out.println("The numbers are: ");

System.out.println(myArray[0]);
System.out.println(myArray[1]);
System.out.println(myArray[2]);
System.out.println(myArray[3]);
System.out.println(myArray[4]);

int temp = myArray[i];
myArray[i] = myArray[j];
myArray[j] = temp;

System.out.println("The numbers, shuffled, are: ");
System.out.println(myArray[0]);
System.out.println(myArray[1]);
System.out.println(myArray[2]);
System.out.println(myArray[3]);
System.out.println(myArray[4]);
}
}
}
``````

Thank you everyone for your support.

A - Explanation

Let's say you take the input values in order as `{'1','2','3','4','5'}`. What shuffling is corrupting the order randomly, so you have to change the position of elements randomly.

In the demo code,

• `swapArrayElement` swaps the elements those that positions are passed as parameters.

• `getRandom` returns a random value between 0 and the `range` which passed to the method as a parameter.

• `shuffleArray` shuffles the array by changing the positions of elements randomly. Please notify that there is an additional `boolean isShuffled[]` array and it is boolean because we have to keep the track of positions whether they are shuffled or not.

• `isArrayShuffled` method, checks that if all positions are shuffled or not.

B - Demo Code

``````import java.util.Scanner;

public class Test {

public static final int ARRAY_LENGTH = 5;

public static void main(String[] args) {
int myArray[] = new int[ARRAY_LENGTH];

Scanner input = new Scanner(System.in);

for(int i = 0; i < myArray.length; i++)
myArray[i] = input.nextInt();

System.out.println("\nThe numbers are: ");
printIntArray(myArray);

shuffleArray(myArray);
System.out.println("\nThe numbers, shuffled, are: ");
printIntArray(myArray);

input.close();  // no memory leaks!
}

// method for printing array
public static void printIntArray(int[] array) {
for(int i = 0; i < array.length; i++)
System.out.printf("%2d ", array[i]);
System.out.printf("%n");    // use %n for os-agnostic new-line
}

// method for shuffling array
public static void shuffleArray(int[] array) {
int range = array.length;
boolean isShuffled[] = new boolean[range];  // store which positions are shuffled

while(!isArrayShuffled(isShuffled)) {
int positionSrc = getRandom(range);
int positionDst = getRandom(range);

swapArrayElement(array, positionSrc, positionDst);
isShuffled[positionSrc] = true;
isShuffled[positionDst] = true;
}

}

public static int getRandom(int maxRange) {
return (int)(Math.random()*maxRange);
}

public static void swapArrayElement(int[] array, int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}

public static boolean isArrayShuffled(boolean[] isShuffled) {
for(int i = 0; i < isShuffled.length; i++)
if(isShuffled[i] == false)
return false;

return true;
}
}
``````

C - Demo Output

``````Please enter 5 numbers:
1 2 3 4 5

The numbers are:
1  2  3  4  5

The numbers, shuffled, are:
4  2  5  1  3
``````