Sunday, September 4, 2011

Math.random

There’s a lot of times, in programming, when you need a number that’s random, or (loosely speaking) unpredictable. 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>
The square bracket means that 0 is in the 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()?
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 I want a number that's either 0 or 1, what do I use? You guessed it:
Math.round(Math.random());
Mind all the brackets! Can't afford to miss even one, or you'll get an error thrown at you.
And what do I do if I want a whole number between 0 and 10?
Well, Math.random() = a number between + including 0 and 0.99999999999 [it goes on infinitely, of course]. So if I just multiply the whole Math.random() thing by 10, I get: a number between 0 and 9.999999999. If I round this, I get a whole number between 0 and 10. So we use:
Math.round(Math.random()*10);
What if I want a whole number between 6 and 117?
Well, Math.random() = a number between + including 0 and 0.9999999999, so if I multiply Math.random() by 117 and round it, I get a whole number between 0 and 117. So that won’t work. The trick is to multiply Math.random() by (117 – 6) = 111, and then add 6! And, of course, round the whole thing again. So you get:
Math.round(Math.random()*111) + 6;
This way, ActionScript takes Math.random(), or a number between 0 and 0.9999, multiplies it by 111 to get a number between 0 and 110.9999999, then rounds 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.round(Math.random()*(b-a)) + a
-Thanks Actionsscriptmoron-