function randomChoice(...args:Array):*{
return args[int(Math.random()*args.length)];
}
// choose randomly from a number of different values
var val:* = randomChoice(10, "Hello", "0xFF0000", 1000);
// choose a random color (in this case red, green or blue)
var randColor:uint = randomChoice(0xFF0000, 0x00FF00, 0x0000FF);
trace(val);
trace(randColor);
The function randomChoice() takes any number of arguments and spits one of them back randomly. Just a handy function to have around… I especially like to use this function to choose from a handful of colors.
Nice one, will come in pretty handy. I always used Math.random() * 0xFFFFFF for random colors but this is nicer because you can set the colors you want to choose from. Obviously you could also create an array and use a random number generated then point to that ID, but who wants to do all that? ๐ This is much sleeker.
Thanks Matt. Yeah, this:
var col:uint = Math.random()*0xFFFFFF;
is a great snippet. You can also try using the bitwise operators to pull out the rgb values. Like this:
var red:uint = (Math.random() * 0xFF ) << 16; var green:uint = (Math.random() * 0xFF ) << 8; var blue:uint = (Math.random() * 0xFF ); That gives you a little more control.