This method is the most efficient I have seen.
var arr=[];
for(var i=0;i<100;i ){
arr[i]=i;
}
arr.sort(function(){ return 0.5 - Math.random() })
var str=arr.join();
alert(str);
Code explanation:
var arr=[];//Create a new array. This is the recommended way. //It is not recommended to use var arr=new Array();
No need to explain this sentence.
for(var i=0;i<100;i ){ arr[i]=i; }//Loop to assign values to the array
The key point is here
Code
arr.sort(function(){ return 0.5 - Math.random() })
//sort is to sort the array
//This is how it works. Each time, two numbers are selected from the array for operation.
//If the parameter passed in is 0, the positions of the two numbers remain unchanged.
//If the parameter is less than 0, swap the position
//If the parameter is greater than 0, do not swap the position
//Next, use the larger number just now to compare with the next one. Sort in this way.
/*Just right. We take advantage of this and use 0.5 - Math.random. The result of this operation is either greater than 0 or less than 0. In this way, the positions are either swapped or not. Of course, greater than or less than 0 appears randomly. So the array is sorted immediately. */
The last two sentences are output for you to see. hehe.