1/**
2 * Shuffles array in place.
3 * @param {Array} a items An array containing the items.
4 */
5function shuffle(a) {
6 var j, x, i;
7 for (i = a.length - 1; i > 0; i--) {
8 j = round(random() * (i + 1));
9 x = a[i];
10 a[i] = a[j];
11 a[j] = x;
12 }
13 return a;
14}
15
16shuffle(array);
1function randomArrayShuffle(array) {
2 var currentIndex = array.length, temporaryValue, randomIndex;
3 while (0 !== currentIndex) {
4 randomIndex = Math.floor(Math.random() * currentIndex);
5 currentIndex -= 1;
6 temporaryValue = array[currentIndex];
7 array[currentIndex] = array[randomIndex];
8 array[randomIndex] = temporaryValue;
9 }
10 return array;
11}
12var alphabet=["a","b","c","d","e"];
13randomArrayShuffle(alphabet);
14//alphabet is now shuffled randomly = ["d", "c", "b", "e", "a"]
15
16
17
1let numbers = [5, 458 , 120 , -215 , 228 , 400 , 122205, -85411];
2numbers = numbers.sort(function(){ return Math.random() - 0.5});
3/* the array numbers will be equal for example to [120, 5, 228, -215, 400, 458, -85411, 122205] */
1/**
2 * Shuffles array in place.
3 * @param {Array} a items An array containing the items.
4 */
5function shuffle(a) {
6 var j, x, i;
7 for (i = a.length - 1; i > 0; i--) {
8 j = Math.floor(Math.random() * (i + 1));
9 x = a[i];
10 a[i] = a[j];
11 a[j] = x;
12 }
13 return a;
14}
15
16
17/**
18 * Shuffles array in place. ES6 version
19 * @param {Array} a items An array containing the items.
20 */
21function shuffle(a) {
22 for (let i = a.length - 1; i > 0; i--) {
23 const j = Math.floor(Math.random() * (i + 1));
24 [a[i], a[j]] = [a[j], a[i]];
25 }
26 return a;
27}
28
29var myArray = ['1','2','3','4','5','6','7','8','9'];
30shuffle(myArray);
1const getShuffledArr = arr => {
2 const newArr = arr.slice()
3 for (let i = newArr.length - 1; i > 0; i--) {
4 const rand = Math.floor(Math.random() * (i + 1));
5 [newArr[i], newArr[rand]] = [newArr[rand], newArr[i]];
6 }
7 return newArr
8};