AS3 Array Sequence Implementation code

Source: Internet
Author: User

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);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.