Shuffle Algorithm
To 1~10 a total of 10 numbers of arrays randomly into the case, I now know that there are two methods, a sort () method and the push () method.
First, the Sort method:
var arr=[1,2,3,4,5,6,7,8,9];
function upset (A, b) {
// with math.random () function Generation 0~1 between the random number and the 0.5 Compare, return -1 or 1
return Math.random () >.5 -1:1;
// simplify the method above
//return 0.5-math.random ();
}
Arr.sort (upset);
X
Sort () Method:
Parameters |
Describe |
SortBy |
Optional. Specifies the sort order. Must be a function. |
If a is less than B, a value that is less than 0 is returned if a should appear before B in the sorted array.
If a equals B, 0 is returned.
If a is greater than B, a value greater than 0 is returned.
Second, push () method;
var arr = [1,2,3,4,5,6,7,8,9,10];
var result = [];
while (Arr.length) {
Result.push (Arr.splice (parseint (Math.random () * arr.length), 1) [0]);
}
Console.log (result);
JS Shuffle algorithm