randomize an array python code example
Example 1: python randomize list
import random
random.shuffle(list)
Example 2: randomly shuffle array python
import random
l = list(range(5))
print(l)
lr = random.sample(l, len(l))
print(lr)
print(l)
Example 3: 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
};