# Random number 1to5

**URL:** <https://forum.kirupa.com/t/random-number-1to5/2874>\
**Category:** Uncategorized\
**Created:** [March 18, 2002, 1:38pm UTC](https://forum.kirupa.com/t/random-number-1to5/2874 "2002-03-18T13:38:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 18, 2002, 1:38pm UTC](https://forum.kirupa.com/t/random-number-1to5/2874/1 "2002-03-18T13:38:09Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 18, 2002, 3:25pm UTC](https://forum.kirupa.com/t/random-number-1to5/2874/2 "2002-03-18T15:25:47Z")

</div>

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}

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 18, 2002, 8:21pm UTC](https://forum.kirupa.com/t/random-number-1to5/2874/3 "2002-03-18T20:21:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 18, 2002, 8:46pm UTC](https://forum.kirupa.com/t/random-number-1to5/2874/4 "2002-03-18T20:46:04Z")

</div>

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:

```auto
 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:

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

```

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

```auto
 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

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

```

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 18, 2002, 8:50pm UTC](https://forum.kirupa.com/t/random-number-1to5/2874/5 "2002-03-18T20:50:27Z")

</div>

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:

```auto
 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]

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 19, 2002, 7:12pm UTC](https://forum.kirupa.com/t/random-number-1to5/2874/6 "2002-03-19T19:12:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 19, 2002, 7:16pm UTC](https://forum.kirupa.com/t/random-number-1to5/2874/7 "2002-03-19T19:16:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 19, 2002, 7:17pm UTC](https://forum.kirupa.com/t/random-number-1to5/2874/8 "2002-03-19T19:17:22Z")

</div>

I quess 🙂 If you want to show off, hehe

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 20, 2002, 7:01am UTC](https://forum.kirupa.com/t/random-number-1to5/2874/9 "2002-03-20T07:01:31Z")

</div>

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. 🙂
