# Math.random in AS3

There’s a lot of times, in programming, when you need a number that’s random, or (loosely speaking) unpredictable. This will come up, for instance, if you have a game in which the throwing of dice is simulated. For that sort of need, ActionScript has a built-in constant, written exactly like so:

Math.random()

The Adobe LiveDocs give the following description of our friend Math.random():

“a pseudo-random number n, where 0 <= n < 1″

That certainly makes it clear, doesn’t it? If you’ve had maths in school, or rather, if you could remember it at all, you’d know that the Math.random() number falls within this interval:

[0, 1>

### Which kind of numbers come out of Math.Random()?

The square bracket means that 0 is in our interval, and the open bracket means that 1 is not. And that means that these are the sort of numbers Math.random() generates:

0
0.374539
0.2
0.999999999
0.45311

Et cetera. So the lowest number you could get is 0, and the highest is very close to, but never quite, 1.

How is that useful to you?

Say you need to simulate the flipping of a coin. Then you need either a 0 or a 1. How could you ever achieve that with Math.random()?

### Three methods in AS3 for getting round numbers

Luckily, there are 3 methods for rounding in ActionScript that you can call to the rescue. They are:

Math.ceil()
Math.floor()
Math.round()

Math.ceil() rounds numbers to the nearest higher integer.
Math.floor() rounds numbers to the nearest lower integer.
Math.round() rounds numbers to the nearest integer, whether it happens to be a higher or a lower one.

If you want to get familiar with this, do the Math.random exercise, and you’ll soon feel comfortable with this stuff.

So if you want a number that’s either 0 or 1, what do I use? You might think that

Math.round(Math.random());

is the solution.Then similarly, to get a number between 0 and 10, you might think you’d just use

Math.round(Math.random()*10);

But, as our astute reader Mr. Davis points out, and I quote that excellent gentleman:

If you use “Math.round(Math.random()*10);” to get a whole number between 0 and 10, the probability of getting 0 and 10 is much less than the probability of getting 1 through 9.

The reason being that if you use Math.round(), you need to get = 9.5 to get 10. So you have a range of .5 to end up with 0 or 10. Whereas the other numbers have a range of 1. Let’s use the number 5 as an example. >= 4.5 and < 5.5 will return 5 when rounded.

The best way to get a range of whole numbers from 0 to 10 (with even probabilities) is using Math.floor(Math.random() * 11);

We’ll draw a line here, with all the rational numbers (supposedly – there are an infinite number of numbers on such a line!) from zero to 10 on it. What happens if you round down all possible numbers on your line ?

As you can see, numbers 1 to 9 have equal likelihood, because they all result from a full segment of our number line. But 0 and 10 didn’t get as good a deal: 0 results only when Math.random() gives us a number between 0 and 0.499…, which is a smaller segment of the line. And 10 results only when Math.random() gives us a number between 9.599… and 9.99…, which again is only a small segment of our numbers line.

This, then, is the reason that Math.round() isn’t very suitable for our purposes. That’s why, as Mr. Davis points out, we must use Math.floor().

Ok – so Math.random() = [0, 1>

Then what is Math.floor(Math.random())? Well, any number from 0 to 0.49999 rounds down to 0. And any number from 0.5 to 0.9999 gives us . Here, the distrubution is equal, and therefore, to get a 0 or a 1, you use

Math.floor(Math.random());

Don’t miss any of the brackets.

What if I want a whole number between, say, 6 and 117?

Math.random()*118 would be [0, 1> times 118 is [0, 118>. The lowest number that could result would therefore be 0, while the highest would be 117. Right? So now we have a number between 0 and 117.

But we need a number between 6 and 117. If I add 6, I get:

Math.floor(Math.random)*118 + 6 = a number between 6 and 123.

Oops – a little too much. I can tweak this by simply using

Math.floor(Math.random)*112 + 6;

This way, ActionScript takes Math.random(), or a number between 0 and 0.9999, multiplies it by 112 to get a number between 0 and 111.9999999, then rounds down those to get a number between 0 and 111, and finally adds 6 to the whole enchilada, resulting in: a number between 6 and 117.Of course you’re not going to remember that. I would never expect you to! I’m just going to provide you with a little formula which you can look up, any time you need it, in the Very Handy Reference. Here it is:

to get a whole number between a and b, use:

Math.floor(Math.random()*(b-a)) + a

Works every time! I promise.

Question or suggestion? Drop a line in comment!

Mr. Davis December 4, 2013 at 12:18 pm

Just wanted to point out that if you use “Math.round(Math.random()*10);” to get a whole number between 0 and 10, the probability of getting 0 and 10 is much less than the probability of getting 1 through 9.

The reason being that if you use Math.round(), you need to get = 9.5 to get 10. So you have a range of .5 to end up with 0 or 10. Whereas the other numbers have a range of 1. Let’s use the number 5 as an example. >= 4.5 and < 5.5 will return 5 when rounded.

The best way to get a range of whole numbers from 0 to 10 (with even probabilities) is using Math.floor(Math.random() * 11);

admin December 4, 2013 at 5:03 pm

Excellent point, Mr. Davis. I’ll include it in the post.