Posted on January 22, 2008 by Grant SkinnerI 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.
Follow @gskinner on Twitter for more news and views on interactive media.
|
|
|
15 Comments
that's really useful, thanks for sharing.
Posted by: Thomas Ruffie on Jan 22, 2008 3:18pm
Source Code: Random Methods Utility Class
Bookmarked your post over at Blog Bookmarker.com!
Posted by: experimental on Jan 22, 2008 3:44pm URL: http://www.blogbookmarker.com/tags/experiment…
very usefull, nice and clean
brgds
Posted by: MrSteel on Jan 22, 2008 4:04pm URL: http://mrsteel.wordpress.com
Super nice, thank you.
Posted by: Justin Rhoades on Jan 22, 2008 10:11pm URL: http://www.jmrhoades.com
thanks for sharing
Posted by: Thomas Fink on Jan 23, 2008 3:58am URL: http://www.perfectscript.de
This is awesome.
Thank you kindly.
Posted by: Dave Stewart on Jan 23, 2008 8:47am 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 on Jan 24, 2008 12:43pm
Thanks for sharing!
Posted by: ian pretorius on Jan 30, 2008 12:27pm
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...
Posted by: Danny on Mar 5, 2008 8:13pm URL: http://www.k2xl.com
Also I think this could work faster for bit
return Number(random()
Posted by: Danny on Mar 5, 2008 8:17pm URL: 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 on Mar 26, 2008 3:57pm URL: http://www.soulwire.co.uk
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
Posted by: jwerre on Jul 31, 2008 10:29am URL: http://asklaunch.com
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.
randomArray.push(Math.floor(Rnd.float(10,event.currentTarget.totalTime)));
Posted by: Greg on Dec 2, 2008 10:18am
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
Posted by: Atilla CALISKAN on Dec 25, 2008 10:49am
very nice:)
Posted by: patrick on Aug 27, 2009 9:54pm URL: http://www.tiannaxin.cn