how to shuffle array code example
Example 1: 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 2: how to randomize an array
const getShuffledArr = arr => {
const newArr = arr.slice()
for (let i = newArr.length - 1; i > 0; i--) {
const rand = Math.floor(Math.random() * (i + 1));
[newArr[i], newArr[rand]] = [newArr[rand], newArr[i]];
}
return newArr
};