I have an array like this:
var arr1 = ["a", "b", "c", "d"];
How can I randomize / shuffle it?
I have an array like this:
How can I randomize / shuffle it? 


The defacto unbiased shuffle algorithm is the FisherYates (aka Knuth) Shuffle. See https://github.com/coolaj86/knuthshuffle You can see a great visualization here (and the original post linked to this)
Some more info about the algorithm used. 


One could (or should) use it as a protoype from Array: From ChristopheD:



Here is a JavaScript implementation of the Durstenfeld shuffle, a computeroptimized version of FisherYates:
The FisherYates algorithm works by picking one random element for each original array element, and then excluding it from the next draw. Just like randomly picking from a deck of cards. This exclusion is done in a clever way (invented by Durstenfeld for use by computers) by swapping the picked element with the current element, and then picking the next random element from the remainder. For optimal efficiency, the loop runs backwards so that the random pick is simplified (it can always start at 0), and it skips the last element because there are no other choices anymore. The running time of this algorithm is O(n). Note that the shuffle is done inplace. So if you do not want to modify the original array, make a copy of it first with Updating to ES6 / ECMAScript 2015The new ES6 allows us to assign two variables at once. This is especially handy when we want to swap the values of two variables, as we can do it in one line of code. Here is a shorter form of the same function, using this feature.



Use the underscore.js library. The method



Adding to @Laurens Holsts answer. This is 50% compressed.






[community edit: This answer is incorrect; see comments. It is being left here for future reference because the idea is not that rare.]



yet another implementation of FisherYates, using strict mode:



A recursive solution:



This variation of FisherYates is slightly more efficient because it avoids swapping an element with itself:





NEW! Shorter & probably *faster FisherYates shuffle algorithm
script size (with fy as function name): 90bytes DEMO http://jsfiddle.net/vvpoma8w/ *faster probably on all browsers except chrome. If you have any questions just ask. EDIT yes it is faster PERFORMANCE: http://jsperf.com/fyshuffle using the top voted functions. EDIT There was a calculation in excess (don't need c+1) and noone noticed shorter(4bytes)&faster(test it!).
Caching somewhere else http://jsfiddle.net/vvpoma8w/2/ Readable version (use the original version. this is slower, vars are useless, like the closures & ";", the code itself is also shorter ... maybe read this How to 'minify' Javascript code , btw you are not able to compress the following code in a javascript minifiers like the above one.)



Randomize array using array.splice()


First of all, have a look here for a great visual comparison of different sorting methods in javascript. Secondly, if you have a quick look at the link above you'll find that the
Edit: as pointed out by @gregers, the compare function is called with values rather than indices, which is why you need to use 


Randomize array


I found this variant hanging out in the "deleted by author" answers on a duplicate of this question. Unlike some of the other answers that have many upvotes already, this is:
Here's a jsfiddle showing it in use.



FisherYates shuffle in javascript. I'm posting this here because the use of two utility functions (swap and randInt) clarifies the algorithm compared to the other answers here.



Just pass the array to function and in return get the shuffled array 


With ES2015 you can use this one:
Usage:


based on the FisherYates Shuffle, you can try this reusable arrayshuffle component. Example:
I also like this Lodash function which returns a new array and leaves the original array unchanged:
(Proper disclosure: I'm on the CoCycles team.) 

Considering apply it to in loco or to a new immutable array, following other solutions, here is a suggested implementation:



Ronald Fisher and Frank Yates shuffle






the shortest


I see no one has yet given a solution that can be concatenated while not extending the Array prototype (which is a bad practice). Using the slightly lesser known
You'd probably want to pass the second parameter
Let's define
You can see it in action in JSFiddle or here:



From a theoretical point of view, the most elegant way of doing it, in my humble opinion, is to get a single random number between 0 and n!1 and to compute a one to one mapping from When computing with IEEE754 double precision floating numbers, you can expect your random generator to provide about 15 decimals. Since you have 15!=1,307,674,368,000 (with 13 digits), you can use the following functions with arrays containing up to 15 elements and assume there will be no significant bias with arrays containing up to 14 elements. If you work on a fixedsize problem requiring to compute many times this shuffle operation, you may want to try the following code which may be faster than other codes since it uses The following function will not be used, but I give it anyway; it returns the index of a given permutation of
The reciprocal of the previous function (required for your own question) is below; it is intended to work with up to 16 elements; it returns the permutation of order n of
Now, what you want merely is:
It should work for up to 16 elements with a little theoretical bias (though unnoticeable from a practical point of view); it can be seen as fully usable for 15 elements; with arrays containing less than 14 elements, you can safely consider there will be absolutely no bias. 


I was thinking about oneliner to paste in console. All tricks with
But don't use it in production code, it's not optimal and works with strings only. 


A very simple way for small arrays is simply this:
It's probably not very efficient, but for small arrays this works just fine :) 





A simple modification of CoolAJ86's answer that don't modify original array



Just to have a finger in the pie. Here i present a recursive implementation of Fisher Yates shuffle (i think). It gives uniform randomness.



Some of the answers could be shortened with the latest ES6. Shuffle Array In place
ES6 allows us to assign two values at once. This is especially handy in line 4 above, where two variables are swapped in one line of code. Leave original Array intact and return a shuffled arrayIf you want to make a more pure function, and leave the original array intact, you can simply duplicate the array and then run the same algorithm.
An ascending AlgorithmThe algorithm below uses an ascending loop. It is less intuitive, yet short and valid.
Testing Reliability of the Randomising functionI used the function bellow to test the reliability of the randomising function. This function prints out the distribution of values in each position.
running the test on both getShuffledArray functions, both in chrome and in node, displayed in the console an even distribution. This is in accordance with what we would expect from a randomizing function. 


All the other answers are based on Math.random() which is fast but not suitable for cryptgraphic level randomization. The below code is using the well known



You can do it easily with map and sort:
You can shuffle polymorphic arrays, and the sort is as random as Math.random, which is good enough for most purposes. 

a shuffle function that doesn't change the source arrayHere are my 5 cents. If you don't wish your shuffle function to mutate the source array, you can copy it to a local variable, then do the rest with a simple shuffling logic.
Shuffling logic: pick up a random index, then add the corresponding element to the result array and delete it from the source array copy. Repeat this action until the source array gets empty. And if you really want it short, here's how far I could get:


