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.
Comments (10)
that's really useful, thanks for sharing.
Posted by: Thomas Ruffie at January 22, 2008 03:18 PMURL:
very usefull, nice and clean
Posted by: MrSteel at January 22, 2008 04:04 PMbrgds
URL: http://mrsteel.wordpress.com
Super nice, thank you.
Posted by: Justin Rhoades at January 22, 2008 10:11 PMURL: http://www.jmrhoades.com
thanks for sharing
Posted by: Thomas Fink at January 23, 2008 03:58 AMURL: http://www.perfectscript.de
This is awesome.
Posted by: Dave Stewart at January 23, 2008 08:47 AMThank you kindly.
URL: http://www.cronin-co.com
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!
Posted by: Dave Sale at January 24, 2008 12:43 PMURL:
Thanks for sharing!
Posted by: ian pretorius at January 30, 2008 12:27 PMURL:
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
Posted by: Danny at March 5, 2008 08:13 PMThat should return -1 or 1...
URL: http://www.k2xl.com
Also I think this could work faster for bit
return Number(random()
Posted by: Danny at March 5, 2008 08:17 PMURL: http://www.k2xl.com
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 :)
Posted by: Justin at March 26, 2008 03:57 PMURL: http://www.soulwire.co.uk