A method of generating a random number that is not repeated (with a shaded instance)

Source: Internet
Author: User
Tags eval
Random | Repeats the number 0 to 99 and does not repeat.
Applies when you do multiple random squares to shade a picture and play the music randomly while playing the animation, but not repeat.
Method One:

Oldtime=gettimer ();
My_array=new Array ();
n=100;
for (Var i=0;i<n;i++) {
My_array[i]=i;
}
for (Var i=0;i<n;i++) {
var tmp1=random (n);
var tmp2=my_array[i];
MY_ARRAY[I]=MY_ARRAY[TMP1];
MY_ARRAY[TMP1]=TMP2;
}
Trace (My_array);
Trace (Gettimer ()-oldtime);//program run time
//----------------------------------------------------------

--------------Method Two-------------------------------------

N=gettimer ();
Array.prototype.randomize = function () {
This.sort (function (A, b) {return random (2) >0? 1:-1;});
}
var myarray = new Array ();
for (Var i=0;i<1000;i++) {
Myarray[i]=i;
}
Myarray.randomize ();
Trace (myarray);
Trace (Gettimer ()-N);

Method Three:

var num_arr:array = new Array ();
var j = 0;
var judge = 0;
do {
var temp_num = Math.Round (Math.random () *100);
for (var i = 0; i<num_arr.length; i++) {
var temp_n = num_arr[i];
if (Temp_num = = temp_n) {
Judge = 1;
Break
} else {
Judge = 0;
}
}
if (judge = = 0) {
Num_arr.push (Temp_num);
j + +;
_root["T" +j].text = Temp_num;
}
while (j<100);
Trace (Num_arr);


Click here to download the source file

Production process: Build two layers, each put a picture, where the top of the picture definition instance name: PIC_MC
Then draw a rectangle, convert it to MC, define the ID name as box, and write the As on the frame:
Creatmask ();
Set up a cover picture
Newnum ();
Generate random numbers
var i =-1;
var n = 0;
_root.onenterframe = function () {
i++;
if (i<300) {
Eval ("_root.m1_mc.box" +my_array[n]). Removemovieclip ();
Remove Square
n++;
}
if (i>=400 && i<700) {
m = my_array[i-400];
_root.m1_mc.attachmovie ("box", "box" +m, m+10, {_x:int (M/15) *20, _y:m%15*20});
Load box
}
if (i>800) {
i = 0;
n = 0;
}
};
//-------------------------------------
function Creatmask () {
_root.createemptymovieclip ("M1_MC", 1);
m1_mc._x = 0;
mc_mc._y = 0;
for (var i = 0; i<300; i++) {
_root.m1_mc.attachmovie ("box", "box" +i, i+10, {_x:int (I/15) *20, _y:i%15*20});
}
Pic_mc.setmask (M1_MC);
}
function Newnum () {
My_array = new Array ();
n = 300;
for (var i = 0; i<n; i++) {
My_array[i] = i;
}
for (var i = 0; i<n; i++) {
var tmp1 = random (n);
var tmp2 = my_array[i];
My_array[i] = My_array[tmp1];
MY_ARRAY[TMP1] = TMP2;
}
Improved on the basis of the above


Click here to download the source file

There is a difference between the production method and the box element in the square into the MC, and then edit the MC, do some movement on the above deformation.
Newnum ();
Generate random numbers
_root.createemptymovieclip ("M1_MC", 1);
m1_mc._x = 0;
mc_mc._y = 0;
Pic_mc.setmask (M1_MC);
for (var i = 0; i<300; i++) {
_root.m1_mc.attachmovie ("box", "box" +i, i+100, {_x:int (I/15) *20, _y:i%15*20});
}
var i =-1;
_root.onenterframe = function () {
i++;
if (i<300) {
m = My_array[i];
Eval ("_root.m1_mc.box" +my_array[m]+ ". P"). Play ();
}
if (i>=400 && i<700) {
m = my_array[i-400];
Eval ("_root.m1_mc.box" +my_array[m]+ ". P"). Play ();
}
if (i>800) {
i =-1;
}
};
//-------------------------------------
function Newnum () {
My_array = new Array ();
n = 300;
for (var i = 0; i<n; i++) {
My_array[i] = i;
}
for (var i = 0; i<n; i++) {
var tmp1 = random (n);
var tmp2 = my_array[i];
My_array[i] = My_array[tmp1];
MY_ARRAY[TMP1] = TMP2;
}
}

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.