Random number 1to5

When i click on a button, there should appear a random number from 1 to 5 in MC or somewhere. It doesn’t matter. (easy isn’t it, but i don’t know).\r\rAnywayz Pom, where have you learned these things?

make a dynamic text field. Give it a variable name of “ranNum”. Place it on the main timeline.\r\rmake a button and place it on the main timeline. Paste the following script on the button.\r\ron(release){\r&nbsp &nbsp &nbsp &nbsp _root.ranNum=Math.round(Math.random()*5+1);\r}

What what what ??? Pom ?? That’s me. What things ? When ? Where ? Whence ?\r\rpom 0]

Talking about things I’ve just learned… Here’s a fall-on-your-ass thing : the undocumented AS functions. The always so amazing Robert Penner (check his site, it’s worth every kb) has written a tutorial about them on ultrashock, and there’s a function that will do what you want, just another way.

The ASClamp() function forces a number to fall within a given range. This is how it is internally defined in the Flash 5 player:

 function ASClamp (v, mn, mx) {\r\r    if (isNaN(v)) {\r\r        v = 0;\r\r    }\r\r    return Math.max( mn, Math.min(number (v), mx) );\r\r}

First, the value v is checked to make sure it is a number. Then, Math.max() and Math.min() are used to force v between the boundaries mn (minimum number) and mx (maximum number). For instance, if v is -2, mn is 10, and mx is 20, ASClamp() will return 10:

 trace ( ASClamp (-2, 10, 20) ); // output: 10

Similarly, a higher number than 20 will be clamped to 20:

 trace ( ASClamp (33, 10, 20) ); // output: 20

ASClamp() is absent from the Flash 6 player. However, it is a useful function that you may consider adding to your library–perhaps under another name, like Math.clamp().
\rThen you could for instance

 trace (ASClamp (Math.random(10),1,5)) ;

Isn’t that great ?\r\rpom 0]

Or even more complicated and unuseful, but so beautiful…

In general, ASNative (i, j) returns a function reference. It’s like all the Flash functions are stored in a spreadsheet, and you can access them by rows and columns with ASNative(). A convenient way to work with ASNative functions is to assign the result to a variable, and then execute the variable as a function. For instance, ASNative (200, 11) returns the Math.random() function. You can retrieve this function using ASNative() and execute it like this:

 func = ASNative (200, 11);\r\rtrace ( func() ); // output: a random # between 0 and 1

There is no real benefit to running Math.random() this way; functions pulled from ASNative() don’t run any faster than normal.
You can check the ASNative tables at Ultrashock’s.\r\rpom 0]

it’s much more simple…random (5) or so is a variable in flash specially made for genrating random numbers :slight_smile:

Of course, but aren’t those functions beautiful ???\rpom 0]

I quess :slight_smile: If you want to show off, hehe

well it’s also the matter that the “random(5);” method has been depreciated and though it is a little faster on the processor, does not produce as random a number as Math.random()*5+1;\r\rthen there are POM’s ideas, which are intriguing to try… I’ve not heard of either yet… but I think that the plain old Math.random function is just easier. :slight_smile: