RSS

AS3 Math

Actionscript 3 | Posted on Sep 15 2010

A very useful reminder for Math function.

n = Math.random();
// 0 <= n < 1

n = Math.round(x);
// rounds x to the closest integer

n = Math.floor(x);
// rounds x downwards to the closest integer

n = Math.ceil(x);
// rounds x upwards to the closest integer

n = Math.round(Math.random()*10);
// 0 <= n <= 10

n = Math.ceil(Math.random()*10);
// 1 <= n <= 10

n = Math.floor(Math.random()*10);
// 0 <= n <= 9

n = Math.random()*10 + 5;
// 5 <= n <= 15

n = Math.round(Math.random())*2 - 1;
// n will randomly be -1 or 1

Post a Comment