AS3 Helper Class – RandomBoolean

We are building an AS3 Helper class to speed up development on Actionscript 3 projects. Feel free to use these methods in your own Helper class, or to share your own methods that you find useful.

RandomBoolean()

1
2
3
4
5
6
7
8
9
10
public class Helper
{
	/**
 	* Returns Boolean true or false, randomly.
 	*/		
	public static function RandomBoolean():Boolean
	{
		return  Boolean(Math.floor(Math.random() * 2));
	}		
}

Usage

1
var coinTossIsHeads:Boolean = Helper.RandomBoolean(); //coin toss will randomly be heads or tails

Comments

Sometimes you just need to randomize between true or false, like a coin toss. Since the code is only one line, it may seem extravagant to make a method out of it, but i find it easy to use this way.

For more AS3 Helper Classses and a pile of other useful stuff, check out our Flash and Actionscript 911 feature.

Leave a Reply

Your email address will not be published. Required fields are marked *