Write yourself an array of random instances.
The code is as follows |
Copy Code |
var _arr:array=new Array (9,8,7,6,5,4,3,2,1); Randomarray (_arr); Trace (_arr);
function Randomarray (Arr:array): Array { Return Arr.sort (Random_fun); function Random_fun (a:*, b:*): int { if (Math.random () < 0.5) { return-1; } return 1; } } |
First, define a function with two formal parameters. Represents the two comparison elements of an array bubbling sort, which returns the random value of 1,0,-1, sorted by the different random values obtained each time the comparison is made, and I select an element (a card) in the array to trace () the sequence number of the tag, Found that the chaos is not very thorough, the idea of whether the function used in order to return the value of a more likely, and then Trace (), the chaos is more thorough, after the situation is changed as follows:
The code is as follows |
Copy Code |
function Sortf (a:movieclip,b:movieclip): int { return int (math.random () *52);//disorderly order, increase the range of returned value, easy to more complete disorderly order } Mcarr.sort (SORTF); Trace (MCARR[0].I); |
Another piece of code that you can test normally:
code is as follows |
copy code |
var arr: Array=[]; for (var i:int=0; i<30; i++) { Arr[i]=i; } Trace (arr); Function Sortf (a,b): int { return Math.floor (Math.random () *3-1);//change *3 to *30 try it, the effect is much better Arr.sort (SORTF); Trace (arr); |