ActionScript3 Dice Class

Written on December 4, 2007 – 8:29 pm | by sascha |

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).

  1. 4 Responses to “ActionScript3 Dice Class”

  2. By TinkNo Gravatar on Dec 4, 2007 | Reply

    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 ;)

  3. By adminNo Gravatar on Dec 4, 2007 | Reply

    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!

  4. By TinkNo Gravatar on Dec 14, 2007 | Reply

    Hey

    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:

    height=”100%”

    or in Flex where the property name states percent such as

    percentHeight=100;

    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. 1 Trackback(s)

  2. Dec 4, 2007: H1DD3N.R350URC3 » Some bits (and code) about rolling dice

Post a Comment

What is this place??

Here I'll share my knowledge, discovery and experience related to my hobby and work. Most articles on this site are related to Game Development, Flash ActionScript, Audio Production, Design and Classic Gaming.

Subscribe RSS Feed?

 Main Feed (contains all categories), Dev Feed, Design Feed, Audio Feed, Gaming Feed, Miscellaneous Feed
Find entries: