Answers for "how to shuffle an array"

1

javascript shuffle an array

const shuffleArray = (arr) => arr.sort(() => 0.5 - Math.random());

console.log(shuffleArray([1, 2, 3, 4]));
// Result: [ 1, 4, 3, 2 ]
Posted by: Guest on September-28-2021
3

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;
        }
    }
Posted by: Guest on September-20-2020
3

how to shuffle an array javascript

/**
 * Shuffles array in place.
 * @param {Array} a items An array containing the items.
 */
function shuffle(a) {
    var j, x, i;
    for (i = a.length - 1; i > 0; i--) {
        j = round(random() * (i + 1));
        x = a[i];
        a[i] = a[j];
        a[j] = x;
    }
    return a;
}

shuffle(array);
Posted by: Guest on September-25-2020
1

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
};
Posted by: Guest on June-12-2020
0

randomize an array

let unshuffled = ['hello', 'a', 't', 'q', 1, 2, 3, {cats: true}]

let shuffled = unshuffled
  .map((a) => ({sort: Math.random(), value: a}))
  .sort((a, b) => a.sort - b.sort)
  .map((a) => a.value)
Posted by: Guest on May-22-2021
0

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;
        }
Posted by: Guest on October-28-2020

Code answers related to "how to shuffle an array"

Code answers related to "Javascript"

Browse Popular Code Answers by Language