Random function detailed

Source: Internet
Author: User
Tags integer lowercase ord pow require
dom| Function | Details about the effect of flash, I think we all want to make that kind of "not only let a person see forget, and see also want to see" effect? "A look forget" is a master's work, "see also want to see" also need a deep foundation and the rich connotation of the work. As our rookie, the nature of the 1:30 all not up to that height. But we can at least be able to make people "every time there is a freshness", what is the key? actionscrpt!

Of course, the title, I want to talk about the random function. As other aspects are left to the master. This is for the rookie to see. Although it is for the rookie to see, but I also assume that the rookie will be a little bit as, at least for my code can be applied to the work.

These days, the Empire Forum's as and novice areas may be Christmas, are asking "How to do Snowflakes", I also gave an example, the master also gave a few source files, one of the key statements is the random () function. The snow itself is good to do, draw a white circle is finished, snowflakes fall can also use a motion gradient ( Had seen a rain before the effect is to do so), the main or snowflakes appear in the position, so we used the random (550) to randomly select a y-coordinate, the x-coordinate of course is 0. So the numerous snowflakes are no longer monotonous "loop play", but never repeat.

OK, so many nonsense above is a primer, here is the point.

Let me start by outlining the random () function and the Math.random () function, and then introduce some of the custom functions that come out of this. For how to combat some effect, That requires the wings of imagination and the support of other as foundations. And the algorithm itself is not difficult. In the end, I'll introduce a simple effect. Hope to inspire the reader's thinking.

1, random (number) function introduction
See the Help document, simply to mention, random (number) returns a random integer between 0~number-1. Parameter number represents

An integer.

Trace (Random (5));//Copy to the first frame of the main scene.

2, Math.random ()
See the Help documentation. Returns the number between 0~1 with 14-bit precision, noting that there are no parameters. I've heard that MM is recommended for this function, not the one above. Personally, I think the former one is a little bit more useful.

Trace (Math.random ());//Copy to the first frame of the main scene.

3, the Custom function
MM to us on these two functions, but there is always a contradiction between demand and supply. The random numbers we sometimes need are not that simple. For example, we want to return a random number with two decimal digits, return a random number between two numbers, return the number of random letters, return a number of random numbers, and so on, all of which require us to write functions to implement. The following code is copied directly to the first frame of the main scene and can be invoked. Note that some functions require entry parameters.

# returns a total of n digits, where m is a random number of decimal digits

function Randomxiao (n,m) {  var a = Math.pow (n+m);  var B = random (a);  Return B=b/math.pow (m);}

You can use Trace (Randomxiao (3,2)) to experiment. This function is very simple. Math.pow (n,m) is used to return a number with N as the base and M as exponent. It's the powers!

# returns a random number between N and M

function Randomnm (n,m) {  if (m>=n) {return    random (m-n+1) +n;  }  else {return    false;  }}

The reason for using random (m-n+1) is because the range of random numbers is m-n, plus 1 allows m to be inside. Plus n guarantees that the random number is lower than N. Plus judgment makes the function more complete. In addition, if you want to return a negative number of random numbers, you can also use RANDOMNM (n,0); Of course, I think more generally with-random (n);

# Returns a letter

function Randomascii () {  var c = string.fromcharcode (random (+65));  if (random (2)) {return    c.tolowercase ();  }  return c;}

Returns a case-insensitive random letter

If you want to return uppercase, you can remove the if condition sentence. If you want to return to lowercase, you can change the conditional sentence to permanent set up, or remove the condition, the last sentence should read: Returns C.tolowercase (); The String.fromCharCode (number) function returns the ASCII code of numbers that represents the digits. toLowerCase () is used to convert uppercase letters to lowercase.

# returns a K-reciprocal random number between N and M

function Randomkdiffer (n,m,k) {  Arrayk = [];  var i = 0;  while (I < k) {   a = random (m-n+1) +n;   for (var j = 0; J < i; J +) {    if (a = = Arrayk[j]) {break      ;    }   }   if (j = = i) {    arrayk[i] = A;    i++    }  }  return Arrayk;}

The element in the array Arrayk is the resulting value. Notice that we borrowed the random (m-n+1) +n to return a random number of n~m. So M itself will be returned. If you want to return the number within m, you can change the n value to 0. If you want to return the uncertain number randomly, you can assign the K value of the entry parameter to K=random (M-n), and the number of random return is not necessarily different, the judgment can be removed, note that the i++ do not miss out. This is no longer given.

#指定若干个字符/number, and then randomly return one (or more) characters/numbers, you can assignments the original word to an array, and then based on the array of subscript to determine the return value. No more functions here, you can try it on your own.

#另需指出, we use MCCOLOR.SETRBG (random (0xFFFFFF) for randomly setting the color value of a MC, which is illustrated in the following example. If you want to specify a color field, you can use the function given above. If you don't know much about a color object, you can look for help, which is not discussed here.

The above functions are directly derived from the random, the following example, can be said to be derived function derivative functions, which will be used directly to the function given above, please note.


function RandomString (n) {  var Arraya = randomkdiffer (1, NUM, n);  var arrayb = "";  for (var i = 0; i < n; i++) {    c=string.fromcharcode (arraya[i]+64);    /* IF (random (2)) {         c=c.tolowercase ();       }    * *    Arrayb = arrayb+c;  }  return Arrayb;}

Note the Stringcharcode method, if you want to write lowercase, write the return value as Arrayb.tolowercase (), and if you return a case-insensitive string, remove the annotation. If you want to return a string that does not specify a length, You can assign the entry parameter to random (n); This function can also be implemented with the RANDOMASCII function, leaving everyone to think for themselves.


For example, select a random number between the two segments of the 1~20,45~70. Because the number of regions is undecided, so it is inconvenient to write directly with a certain function, we want to use the method is directed by the switch statement, we give a function, return a 1~20,45~70, the other area readers please change.

function Randomarea () {  var a=random (2);  Switch (a) {case    0: Return     randomnm (1,20);    Case 1: Return    randomnm (45,70);  

Note that we do not write the entry parameters, but we determine the two paragraphs directly in the function, and the scope is OK. If it is three paragraphs, change to A=random (3); Just add one more case. Of course, you can also set the range of paragraph number as the entry parameter, here is no longer an example. But doing so may increase the number of parameters, I personally do not like a need many parameters of the function. Similarly, we can also randomly return a number of letters or letters or letters plus numbers. Method is also just a combination of the first few functions. Here, for example, returns a random letter of the specified uppercase paragraph. Note that the ASCII code a~z of the lowercase letters correspond to the 97~122 respectively.

function Randomaarea (a,b) {  if (ord (a) <= ord (b) && 65<=ord (a) && ord (b) <=) {return    S Tring.fromcharcode (RANDOMNM (Ord (a), Ord (b)));  else {return    false;  }}

It uses a function ord (char), which is an deprecated function. The ASCII code that returns the char character.

If you want to call a function anywhere, you need to change it a little bit, changing the function we write to a global function. This allows you to call the system function without having to identify the path. For example: function Randomxiao If you want to declare as a global function, you need to change the first line to the following:

_global.randomxiao=function (n,m) {//statment}

The concept of global functions not very clear friends do not have to be intimidated by this noun. This changed the function after the first line, in any place, such as in a MC, direct use ( right, direct use, without adding _root path ) Randomxiao (n,m) can be.

Interested friends can also derive other functions from these functions, such as a random string with numbers and English letters.

Say so much, why did not say Math.random ()?

Because in the next thought, this method is too "professional", in the process of ordinary doing effect or using random (n) a little better. MM Why recommended I do not know.

If you have friends who are not clear, you can download the source file to see. For convenience, the functions in the source file have been changed to global functions. Can be copied directly into your work, of course, can also be written in an external file.

source File Download

4. Example Application

Think about it, as if the case is no good to say, because once you speak up, there must be a statement of the sentence, the algorithm description, there is no sparkle, the key or a creative, another is the basic skills: as a familiar degree. But for the sake of completeness, I still give an example, inside the code is very simple, the picture I am not talented, if you are interested to change, I believe the effect is very good.

First look at the effect:

The effect comes from a Web site, and the color of the strip is changed by me, because it just explains the usage of SETRGB.

First, post the code in the Strip:

Onclipevent (load) {  i = 0;  A = random (5);  if (a = = 0) {    a = 1;  }  col = new Color (this);} Onclipevent (enterframe) {  this._x + = A;  i++;  if (i >=) {    Col.setrgb (random (0xFFFFFF));    A = random (5);    if (a = = 0) {      a = 1;    }    Trace (a);    i = 0;  }  if ((this._x+this._width) <= 0) {    this._x = 550+this._width;  }  if ((this._x-this._width) >) {    this._x = 0-this._width;  }}

As you can see, bar (bar too vulgar, renamed Bar) has two event methods, one load, one enterframe. Typically, load is used to initialize variables that are triggered when the MC is loaded. Enterframe, however, executes the code every time a frame is played. Explain the variables first. I control the number of frames. As you can see, because each frame executes the code in the Enterframe, and there are i++, so this is used to record the number of frames that have been played. A is used to set the speed, because there is this._x+=a in the Enterframe; As mentioned earlier, the Enterframe event is the MC that executes the code once per play frame, so the horizontal axis of bar will change a pixel each time it is played. Col is a color object, the role is to change the color of the MC. The Col=new Color (this) means defining the object and associating it with the current MC object. If it's not a good idea, we'll mention it again.

Again, the Load event is executed at the time the MC is loaded, so it's only executed once (you don't need to load the same object constantly?). , and Enterframe is executed per frame, or it can be said that the code in Enterframe is executed at frame rate. For example, the frame rate is 12, then 12 times per second. It can be used to make objects move, or more commonly, bounce-ball effects. But that's beyond the scope of our discussion.

Next is the code in the Enterframe. As I have said, This._x+=a and i++; , these two are the main. The following is a conditional control statement. The first if is to determine the number of frames that have been played, when 36 frames are played (here is just three seconds, note that if the MC itself has only one frame, or only one frame in the main scene, the Enterframe event is still executing at frame rate, so the above overall explanation is more appropriate), Re-determine the color of the MC: Col.setrgb (Random (0xFFFFFF)); Before we said, Col is a color object, and has been associated with the current MC, then, Col represents the current MC color. I don't know if it's better to understand that. And the object has a Setrgb method, can set up the color value of MC. Here we use the random number of 16, and FFFFFF is the maximum value in the color value. Casually speaking, FFFFFF is white. FF0000 is red. 000000 is black.

The following sentence A=random (10)-5; Is the value of a, which is the speed of the MC. Of course, you can use our fixed function randomnm to specify a range, but I don't want to write a function here. The expression is to get a random number between the -5~4, the reason is negative, because we want to appear that can move around the effect, rather than in one direction.

The following two sentences define the range of motion, and if it goes beyond the left and right edges, let it out from the other end.

Well, it seems that we have written so many functions that are not used. Yes, there is no use here, but there will be a place. As long as you're willing to let your imagination soar.

Wait, there is also a round round effect did not say it! Sorry, this code is left for you to see, than the first simple, because I almost copied the first code.

Many effects must be about copying movie clips, such as snow and rain, and randomly controlling where they appear when copied. It is also best to use Enterframe to control the path of motion. But copying is not the point we're talking about, it's just a reminder. In addition, the combination of the mouse to produce a random effect is also very cool, and the production is usually not complex.

Make a star, or a group of mosquitoes (as long as you want) by using the random + copy + loop of as as a side dish. As long as you know how to use these basic statements correctly.

source File Download

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.