05 June 2011

AS3 handy weighted random & rounding functions with decimal/floating point precision

Like a lot of the "improvements" Adobe made to Actionscript with AS3, the Math.random() function is now a pain in the ass. It only generates a floating point number between 0 and 1 so 99% of the time, you need a few lines of code to get your random on.
With time saving in mind, I've written this weighted random function which makes beautiful randomness a lot handier to implement. For best results throw this in a class package and include it in every project.
Here it comes...



Please see right hand pane before copying my code >>

function round(num:Number, precision:int):Number{
   var decimal:Number = Math.pow(10, precision);
   return Math.round(decimal* num) / decimal;
}

function rand(to:Number, from=0, decimalPlaces=0, weightedRange=0, weightStrength=0):Number{
   var ret
   if(to == from){return(to);}
 
   if(weightedRange && Math.random()<=weightStrength){
      ret = round( Math.random()*(weightedRange[1]-weightedRange[0]) + weightedRange[0], decimalPlaces )
   }else{
      ret = round( Math.random()*(to-from)+from, decimalPlaces )
   }
   return(ret)
}

//USAGE:
rand(100)                   //return random number 0-100 INCLUSIVE
rand(10, -5)                //return random number between -5 and 10
rand(10, -5, 2)             //return random number between -5.00 and 10.00
rand(10, 6, 0, [0,5], 0.9)  //90% chance of returning number between 0-5, 10% chance of returning number between 6-10

12 comments:

Anonymous said...

Hy, can you please send me your e-mail address, i want to ask you something it is not connect with this and don'y know how to contact except in this way? My is nidvanitri@msn.com

Anonymous said...

This is a very helpful function, thanks!

Pnane Weg said...

jordan 6 black infrared
north face coats
hermes belt
fitflop outlet store usa
under armour outlet
burberry scarf
gucci shoes
coach handbags
burberry sale
kevin durant shoes
north face jackets
converse shoes sale
coach factory outlet
air jordan 14
coach factory outlet online
louboutin outlet
fitflop sale
nike nfl jerseys 2015
north face outlet store
ugg boots
reebok outlet
michael kors handbags
christian louboutin sale
north face jackets clearance
new kevin durant shoes
nike clearance
toms outlet
dior handbags
michael kors outlet store
fitflops clearance
kate spade sale
jordan 12
ugg boots
coach purses sale
gucci outlet
wholesale jordans
canada goose clearance
1030wjl

Pnane Weg said...

1 columbia outlet
nike air max
polo shirts
polo shirts for women
north face outlet 70% off
cheap michael kors handbags
air jordan retro 6
salvatore ferragamo outlet
north face clearance
mbt shoes clearance outlet
reebok shoes outlet
polo ralph lauren factory store
celine handbags
christian louboutin shoes
michael kors handbags
fitflops online
jordan 6
ralph lauren extra 25% off
michael kors outlet
air jordan 11
coach factory online sale
jordan shoes
kd shoes
coach handbags outlet
coach shoes for women
gucci outlet
abercrombie and fitch outlet
ferragamo shoes outlet
air jordan 11 legend blue
michael kors factory outlet online
michael kors store
all jordan shoes
ugg boots sale
fitflop sandals for women
gucci outlet
cheap canada goose coats
nike running shoes
1030wjl

geometrydash said...

Greetings! I know this is somewhat off topic but I was wondering if you knew where I could locate a captcha plugin for my comment form? I'm using the same blog platform as yours and I'm having problems finding one? Thanks a lot!
unblocked games
unblocked games at school
unblockedgames

hường lê said...

Such a very useful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article.

Discover our website bounty of free online games now!
Our website has the biggest collection of free online games. Totally new games are added every day!

age of war 2| gold Miner 2| unfair Mario 2 | cubefield 2 |tanki Online 2


Meiqing Xu said...

canada goose
coach factory outlet
coach outlet
ray ban sunglasses
fit flops
canada goose jackets
kate spade outlet
cheap oakleys
birkenstock sandals
birkenstocks
20161228caiyan]
\

bui loan said...

This is a great website. Good sparkling user interface and very informative blogs. I will be coming back in a bit, thanks for the great article. I have found it enormously useful. Clash of Kings download, Photo Grid Free, Douchebag Workout 2, Happy Wheels 4

chenlili said...

coach outlet
michael kors outlet
nfl jerseys wholesale
christian louboutin outlet
coach outlet online
canada goose outlet
canada goose outlet store
canada goose outlet
christian louboutin sale
pandora outlet
shenyuhang20180608

柯飞敏 said...

20180623xiaoke
polo ralph lauren outlet
air max 2018
cheap jordans
canada goose jackets
coach outlet store online
dsquared2 jeans
cheap jordan shoes
oakley sunglasses wholesale
michael kors outlet clearance
coach outlet store online

Obat Atasi Kandung Kemih Lemah said...

Article very interisting, thanks for sharing information

Obat Untuk Mengikis Batu Ginjal
Cara Mengobati Benjolan Di Tangan
Pengobatan Atasi Radang Usus Buntu
Cara Mengobati Radang Pembuluh Darah
Cara Alami Mengobati Ginjal Bengkak
Pengobatan Fistula Ani Tanpa Operasi

icha charisa said...

Hello, we are from the health site admin, want to share our latest articles :

cara menghilangkan benjolan tumor
cara menyembuhkan radang lambung
cara menghilangkan benjolan di kelopak mata
cara mengobati batu empedu
cara mengobati kencing nanah
cara mengobati tipes
cara menghilangkan benjolan di belakang kepala

Post a Comment

Cron Design Studio: Dublin based web design & software development