Math.random();
produces an evenly distributed random number between 0 (inclusive) and 1 (exclusive)
Example output:
function randomMinMax(min:Number, max:Number):Number {
return (min + (Math.random() * Math.abs(max - min)));
}
This function is called by passing a range of minimum and maximum values.
Example:
randomMinMax(1, 10);
Example outputs:
function randomAngle():Number {
return (Math.random() * 360);
}
Example outputs:
Assuming we have an array myArray
:
var value:* = myArray[int(Math.random() * myArray.length)];
Note we use int
to cast the result of Math.random()
to an int because values like 2.4539543
would not be a valid array index.
First define the circle radius and its center:
var radius:Number = 100;
var center:Point = new Point(35, 70);
Then generate a random angle in radians from the center:
var angle:Number = Math.random() * Math.PI * 2;
Then generate an effective radius of the returned point, so it'll be inside given radius
. A simple Math.random()*radius
won't do, because with this distribution the produces points will end up in the inner circle of half radius half of the time, but the square of that circle is a quarter of original. To create a proper distribution, the function should be like this:
var rad:Number=(Math.random()+Math.random())*radius; // yes, two separate calls to random
if (rad>radius) { rad=2*radius-rad; }
This function produces a value that has its probability function linearly increasing from 0 at zero to maximum at radius
. It happens because a sum of random values has a probability density function equal to convolution of all the random values' individual density functions. This is some extended maths for an average grade person, but a kind GIF is presented to draw a graph of convolution function of two uniformed distribution density functions explained as "box signals". The if
operator folds the resultant function over its maximum, leaving only a sawtooth-shaped graph.
This function is selected because the square of a circle strip located between radius=r
and radius=r+dr
increases linearly with increasing r
and very small constant dr
so that dr*dr<<r
. Therefore, the amount of points generated close at the center is smaller than the amount of points generated at the edge of the circle by the same margin as the radius of center area is smaller than the radius of the whole circle. So overall, points are evenly distributed across the entire circle.
Now, get your random position:
var result:Point = new Point(
center.x + Math.cos(angle) * rad,
center.y + Math.sin(angle) * rad
);
To get a random point ON the circle (on the edge of the circle of a given radius), use radius
instead of rad
.
PS: The example ended up being overloaded by explanation of maths.
function randomAngleRadians():Number
{
return Math.random() * Math.PI * 2;
}
Example outputs:
If you need to roll for a true
or false
in an "x% chance" situation, use:
function roll(chance:Number):Boolean {
return Math.random() >= chance;
}
Used like:
var success:Boolean = roll(0.5); // True 50% of the time.
var again:Boolean = roll(0.25); // True 25% of the time.
To get any random color:
function randomColor():uint
{
return Math.random() * 0xFFFFFF;
}
If you need more control over the red, green and blue channels:
var r:uint = Math.random() * 0xFF;
var g:uint = Math.random() * 0xFF;
var b:uint = Math.random() * 0xFF;
var color:uint = r << 16 | g << 8 | b;
Here you can specify your own range for r
, g
and b
(this example is from 0-255).
var alphabet:Vector.<String> = new <String>[ "A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N",
"O", "P", "Q", "R", "S", "T", "U",
"V", "W", "X", "Y", "Z" ];
while (alphabet.length > 0)
{
var letter:String = alphabet.splice(int(Math.random() *
alphabet.length), 1)[0];
trace(letter);
}
Example output:
V, M, F, E, D, U, S, L, X, K, Q, H, A, I, W, N, P, Y, J, C, T, O, R, G, B, Z
var alphabet:Array = [ "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" ];
for (var i:int=alphabet.length-1;i>0;i--) {
var j:int=Math.floor(Math.random()*(i+1));
var swap=alphabet[j];
alphabet[j]=alphabet[i];
alphabet[i]=swap;
}
trace(alphabet);
Example output
B,Z,D,R,U,N,O,M,I,L,C,J,P,H,W,S,Q,E,K,T,F,V,X,Y,G,A
This method is known as Fisher-Yates array shuffle.