how to randomize an array in java code example
Example 1: how to create a random number in java
import java.util.Random;
class scratch{
public static void main(String[] args) {
Random rand = new Random();
System.out.println( rand.nextInt(5) );
}
}
Example 2: how to randomize an array java
void shuffle(int [] arr) {
for (int i = 0; i < arr.length; i++) {
int index = (int) (Math.random() * arr.length);
int temp = arr[i];
arr[i] = arr[index];
arr[index] = temp;
}
}
Example 3: random suffling java
for (int i = myList.length - 1; i > 0; i--){
int j = (int) (Math.random() * (i + 1));
double temp = myList[i];
myList[i] = myList[j];
myList[j] = temp;
}