Source Code: Random Methods Utility Class

I thought I’d share the Rnd class I use for all of my experimental work. It’s a very simple collection of utility methods for working with random values, but it has been extremely useful for me. Here’s the list of methods:

// random();
// returns a number between 0-1 exclusive.
public static function random():Number
// float(50); // returns a number between 0-50 exclusive
// float(20,50); // returns a number between 20-50 exclusive
public static function float(min:Number,max:Number=NaN):Number
// boolean(); // returns true or false (50% chance of true)
// boolean(0.8); // returns true or false (80% chance of true)
public static function boolean(chance:Number=0.5):Boolean
// sign(); // returns 1 or -1 (50% chance of 1)
// sign(0.8); // returns 1 or -1 (80% chance of 1)
public static function sign(chance:Number=0.5):int
// bit(); // returns 1 or 0 (50% chance of 1)
// bit(0.8); // returns 1 or 0 (80% chance of 1)
public static function bit(chance:Number=0.5):int
// integer(50); // returns an integer between 0-49 inclusive
// integer(20,50); // returns an integer between 20-49 inclusive
public static function integer(min:Number,max:Number=NaN):int

Here’s a quick example of how I use it:

// vary this mySprite's rotation by between 10-40 degrees.
// rotate clockwise 30% of the time, counterclockwise 70% of the time.
mySprite.rotation += Rnd.sign(0.3)*Rnd.float(10,40);

You can download the Rnd class by clicking here.

I will be releasing my Rndm class tomorrow, which exposes the same interface for a seeded random engine.

Grant Skinner

The "g" in gskinner. Also the "skinner".



  1. that’s really useful, thanks for sharing.

  2. Source Code: Random Methods Utility Class

    Bookmarked your post over at Blog!

  3. very usefull, nice and clean


  4. This is awesome.

    Thank you kindly.

  5. Hi Grant,

    Just found your blog and wanted to say hi and say I dig your code. I wonder if I’ve seen some before as the style is distinctive and appealing to me in it’s use of randomness and organic nature. It’s great to see this class. Seems like your hitting it here. I toyed with your vein script to see if I could get the tree. No. Well I have trees but they aren’t very reliable trees. So this is it. Great work!

    Why I think your Tree stands out is that it has an organic sense as compared to the more “programmed” graphic builds out there. Which is, to me, night and day.

    And you are wrong, it is very artistic, the beauty is in those “Magic Numbers” somewhere; that you are roping in.

    Thanks for sharing your experience and process!

  6. Thanks for sharing!

  7. Nice class, and I have one suggestion.

    It may be faster to do the sign function like this:

    return Math.floor(random()*2)*-2+1;

    I remember in AS 2 I always did random(2)*-2+1

    That should return -1 or 1…

  8. Also I think this could work faster for bit

    return Number(random()

  9. Hi Grant,

    Very handy! Perhaps a useful edition might be ‘Rnd.element(array)’ ? Something like:

    public static function element( array:Array ):* {

    return array[ integer( array.length ) ];


    I have a similar set of methods which I use all the time, and a simple shortcut for fetching a random element from an array often comes in handy for experiments!

    I’m amazed by your branching experiments! And really enjoyed your talk at FOTB 2007. Keep up the good work 🙂

  10. It though this might be a cool little add on to your Rnd class. It returns an Array of random numbers of a specific length.

    public static function getMultipleRandoms( total:int, min:Number, max:Number=NaN ) : Array


    var tempArray:Array = new Array();

    for (var i:int = 0; i

  11. Thanks for the random number class I am using it in a training video player to generate a random interval with which I verify the viewer is still watching.


  12. Thanks for your code here is another function to get unique array at desired length


    public static function uniqueArray(len:Number, min:Number, max:Number=NaN):Array {

    if (isNaN(max)) { max = min; min=0; }

    var tempArray:Array = new Array();

    for(var i:Number = 0; i

Leave a Reply

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