var allNoGameFriends:Array = [1,2,3,4,5,6,7,8,9] var list:Array = new Array(); var newArr:Array = new Array(); //产生0~i-1的不重复的i个随机数 for (var i:int=0; i<allNoGameFriends.length; i++) { list.push(i); } while (list.length>0) { var index:int = Math.floor(Math.random() * list.length); newArr.push(allNoGameFriends[list[index]]); //选出一个之后去掉这个数 list.splice(index, 1); } allNoGameFriends = newArr.concat(); trace(allNoGameFriends)
function taxis(element1:*,element2:*):int { trace(element1+","+element2) var num:Number = Math.random(); if (num<0.5) { return -1; } else { return 1; } } var arr:Array = ["c","b","a","3","2","1"]; //trace("原来的数组:"+arr); //输出:c,b,a,3,2,1; arr.sort(taxis); //trace("第一次排序:"+arr); //随机排序后的数组arr ; arr.sort(taxis); //trace("第二次排序:"+arr); //随机排序后的数组arr ; arr.sort(taxis); //trace("第三次排序:"+arr); //随机排序后的数组arr ; arr.sort(); //trace("按 Unicode 值排序:"+arr);//输出:1,2,3,a,b,c