Design & Download
Your JS Library
in Seconds!!!

Snippet: randomize() - Randomize Array Values

Written by Christopher West (cwest) on November 29, 2015.
Get a new array of the values in an array (or array-like object) but with the values randomized.
function randomize(arr) {
  arr = slice(arr);
  for (var t, j, l = arr.length, i = l; i--;) {
    t = arr[i];
    arr[i] = arr[j = ~~(Math.random() * l)];
    arr[j] = t;
  }
  return arr;
}

randomize(...) API Documentation

Get a new array of the values in an array (or array-like object) but wit the values randomized.

Parameters

  1. arr {Array|Object}:
    The array (or array-like object) whose values will be placed in a random order.

Returns

A proper array with the same values as found in arr, but where the ordering of values in different.