Could you do something like the example shown below
public class Main {
public static void main(String[] args) throws Exception {
Integer[] input = new Integer[] { 1, 2, 3 };
Integer[] shuffleInput = shuffle(input);
for (int i=0; i<shuffleInput.length; i++) {
System.out.print(shuffleInput[i]);
}
}
public static Integer[] shuffle(Integer[] input) {
Integer[] inputCopy = input.clone();;
Integer[] output = new Integer[input.length];
for (int i=0; i<output.length; i++) {
// Find and get a random element
int randPicker = (int)(System.currentTimeMillis() % inputCopy.length);
output[i] = inputCopy[randPicker];
// Remove selected element from copy
Integer[] aux = new Integer[inputCopy.length - 1];
System.arraycopy(inputCopy, 0, aux, 0, randPicker);
if (inputCopy.length != randPicker) {
System.arraycopy(inputCopy, randPicker + 1, aux, randPicker, inputCopy.length - randPicker - 1);
}
inputCopy = aux;
}
return output;
}
}
This code takes a list of Integer
of any size as input, and return a list with the mixed values.
If the list will always be 3 numbers, it could be a simpler version.
EDITED: This version does not use Math.random()
or any other java.util
.