Usually the most we use is to put an irregular array in order from large to small or small to large, but sometimes we may encounter an ordered array of chaos to achieve the effect of random sorting, this is what I want to introduce to you today;
First I looked for some on Baidu, found a more common method
1 function Randarray (arr) {2 return arr.sort (function() {3 return math.random ()-0.5; 4 }); 5 }
However, there are some problems with this approach, even errors, reference links: https://www.h5jun.com/post/array-shuffle.html
And here are two more practical ways to introduce to you:
Method One:
function Randarray (a) { var b = []; while (A.length > 0) {//iterate over an element in a array each time, and then delete the element until the length of the A array is 0;var index = parseint (Math.random () * ( A.length-1)); B.push (A[index]); 1); } return b;}
Method Two:
function Randarray (a) { var len = a.length; for (var i = 0; i < len-1; i++) { var index = parseint (Math.random () * (Len- i)); c12/>var temp = A[index]; = a[len-i-1]; -I-1] = temp; } return A;}
JavaScript implements an array of random order