This article mainly shares with you the detailed explanation of js disrupting the order of arrays. There are two methods. I hope it can help everyone.
//方法一:也是最简单的方法 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);
//方法二: Fisher–Yates洗牌算法var arr = new Array(1,2,3,5);Array.prototype.shuffle = function() { var array = this; var m = array.length, t, i; while (m) { i = Math.floor(Math.random() * m--); t = array[m]; array[m] = array[i]; array[i] = t; } return array; } arr.shuffle()
Related recommendations:
Disordering the order of numbers and associative arrays in php_PHP tutorial
The above is the detailed content of Detailed explanation of js disrupting the order of arrays. For more information, please follow other related articles on the PHP Chinese website!