Home > Dev, Featured, Random Picks > ActionScript3 Dice Class

ActionScript3 Dice Class

In game development randomness is often necessary for certain tasks, be it the random distribution of graphic tiles, a random factor in NPC AI or random stats in a roleplaying game. Especially for the latter purpose the static Dice class provides a set of methods to roll dice as it is common in a Role-playing game, to be exact four-, six-, eight-, ten-, twelve-, twenty-sided and percentile dice.

The Dice class (and it’s supporting classes) are rather elaborated, using for example the Linear Congruential algorithm in the process of generating ‘true’ random numbers so it might not be the most speed-optimized method for calculating random numbers. For absolute performance the LCA and rounding routines can be removed to speed up calculations.

Using the Dice class is very simple! For example rolling two ten-sided dice can be done with the following call …

var result:int = Dice.tenSided(2);

… rolling the percentile die is even simpler as it does not need any arguments. It always returns a value between 1 and 100 …

var result:int = Dice.percentile();

… the class also provides the roll method with that any x-sided die could be rolled, 3 sixteen-sided dice for instance …

var result:int = Dice.roll(16, 3);

The following small Flex application uses the Dice class and can be used to test dice throw probability according to the bell curve (the more dice are used the lower the probability to roll boundary results).


Download AS3 Dice Class v1.0.0 (6.89 kB, downloaded 1424 times)

Update: The Dice class is now part of the hexagonLib and can be found here.

  1. December 4th, 2007 at 21:06 | #1

    Cool stuff

    I think Adobe made the right move in AS 3.0 using 0 to 1 for all percents unless the % symbol is tag on. 1 is clearly the whole and therefore 100%. 100 is 10000%.

    Because of the roll() method I don’t see any need for methods like tenSided(2). Am I missing something.

    It would also be nice if the result was an Array showing the int for each dice that was rolled so these could be mapped to visuals if required instead of it totally all dice in the result.

    Just some ideas ;)

  2. December 4th, 2007 at 22:18 | #2

    Tink, 100 is 10000%? Did I miss some important AS updates?

    Yes you could use the roll() method only if you want to save yourself a method call. The xSided() methods are just added for convenience and to make code look more clear.

    The idea with returning an array with rolled ints surely is useful for certain operations. Maybe I’ll add that later. Thanks for pointing that out!

  3. December 14th, 2007 at 23:05 | #3


    All alpha values etc now use 0-1 (in fact anything that requires a percent), unless it explicitally uses a percent value in the property such as MXML:


    or in Flex where the property name states percent such as


    When expression percentages as numbers

    -100 is 100 wholes, which expressed as a percent is 10000%.
    – 0.5 is half a whole, which expressed as a percent is 50%.

  1. December 4th, 2007 at 20:29 | #1
You must be logged in to post a comment.