1、
var arr = [1,2,3,4,5,6,7,8,9,10];
function randSort1(arr)
{
for(var i =0,len = arr.length;i < len; i++ ){
var rand =parseInt ( Math.random()*len); // 返回0~9之间的随机数值
var temp = arr [ rand ];
arr [ rand ] = arr[i]; //这里不写,数组就不是原数组的那些值
arr [ i ] = temp;
} return arr;
}
console.log(randSort1(arr));
2、
var arr = [1,2,3,4,5,6,7,8,9,10];
function randSort2(arr){
var mixedArray = [ ];
while(arr.length >0) {
var randomIndex = parseInt(Math.random()*arr.length); //0~arr.length之间的随机数作为索引
mixedArray.push( arr[ randomIndex ] );
arr.splice(randomIndex,1);
} return mixedArray;}
console.log(randSort2(arr));
网友评论