RandomUtil

Generating a random number is a common task most developers will come across. As we all find out the simplest way to accomplish this task is to use the Math.floor and Math.random function to generate our random number.

Math.floor( Math.random() * (maxNum + 1) );

Instead of having to remember this basic formula again and again I created the RandomUtil static class that has convenience methods for creating not only a random number from 0 to x but also a random number for any given range.

Usage

  1. Import the RandomUtil class
    import com.mdbitz.utils.RandomUtil;
  2. Generate a random number
    • Generate a random number from 0 to 10
      var rand:Number = RandomUtil.randomNum( 10 );
    • Generate a random number between 13 and 27
      var rand:Number = RandomUtil.randomNumInRange( 13, 27 );

Download

Date Released Version Download Library
05-25-2010 1.0.0 RandomUtil

Comments & Questions

Add Your Comment