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

20 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!

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

Unknown 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]
\

Unknown 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

India Motorcycle Tour said...

Great article, Very interesting to read this article, I would like to thank you for writing this helpful article share with us.
Motorcycle Tour in Mumbai

Unknown said...

Thanks for this awesome article Very interesting to read this article, Thank you sharing with us.
Shipping Company in India

Mobile app development company in toronto said...

Thank You! For sharing such a great article, It’s been a amazing article.
It’s provide lot’s of information, I really enjoyed to read this.

Mobile App Development in Noida said...

Thanks for the article may be useful for everything

Unknown said...

we have provide the best fridge repair service.
fridge repair in faridabad
Videocon Fridge Repair in Faridabad
Whirlpool Fridge Repair in Faridabad
Hitachi Fridge Repair In Faridabad
Washing Machine Repair in Noida
godrej washing machine repair in noida
whirlpool Washing Machine Repair in Noida
IFB washing Machine Repair in Noida
LG Washing Machine Repair in Noida

CRM said...

I would like to thank you for the efforts you have made in writing this post. Thanks for posting Really Such Things. I should recommend your site to my friends. If you are interested for CRM Software visit here:- CRM Software in Mumbai

dewatampan said...

Situs hondaqq ialah agen poker v partner kami yang sama seperti bandar poker v lainnya yaitu gelangqq, gesitqq, seniqq, pokerwalet, dan sbototo

anonymus said...

Poker online situs terbaik yang kini dapat dimainkan seperti Bandar Poker yang menyediakan beberapa situs lainnya seperti http://62.171.128.49/hondaqq/ , kemudian http://62.171.128.49/gesitqq/, http://62.171.128.49/gelangqq/, dan http://62.171.128.49/seniqq. yang paling akhir yaitu http://62.171.128.49/pokerwalet/. Jangan lupa mendaftar di panenqq

Tekniko Global said...

Tekniko Global is a Top mobile app development company in Noida, Delhi. We have core expert for IOS app & Android application development. We provide Custom Mobile App development services at reasonable cost.
mobile app development company in delhi

Stan Sidler said...

thank you for this post! I will share it on my facebook page? Can I do it? Maybe even buy facebook shares from this site https://soclikes.com/ if you don't mind

vé máy bay đi Nga said...

Mua vé máy bay tại Aivivu, tham khảo

vé máy bay đi Mỹ tháng nào rẻ nhất

vé máy bay đi sài gòn hà nội

đặt vé máy bay đi hà nội vietnam airline

vé máy bay đi đà lạt khứ hồi

giá vé máy bay đi Huế

taxi sân bay giá rẻ

combo đà nẵng 2 ngày 1 đêm

Anonymous said...

no deposit bonus forex 2021 - takipçi satın al - takipçi satın al - takipçi satın al - tiktok takipçi satın al - instagram beğeni satın al - instagram beğeni satın al - google haritalara yer ekleme - btcturk güvenilir mi - izlenme-satin-al.com - numarasmsonay.com - borsagazete.com - takipcisatinals.com - izlenme-satin-al.com/youtube - google haritalara yer ekleme - altyapısız internet - mikrofiber havlu - forexbonus2020.com - tiktok jeton hilesi - tiktok beğeni satın al - microsoft word ücretsiz indir - misli apk indir - binance güvenilir mi - takipçi satın al - mikrofiber havlu - uc satın al - takipçi satın al - takipçi satın al - finanspedia.com

whanou said...

Regardez ceci https://www.dolabuy.su Notre site Louis Vuitton Dolabuy Lire l'article complet réplique dolabuy

tesat said...

j5a41m0j21 a5l13f9b00 h5s84y0j69 l6t60z4y26 l4h16l0g83 g1p00q8n17

valedaane said...

Wild Casino has an FAQs 파라오 카지노 page for frequent queries and concerns, but you'll be able to|you presumably can} always send them an e-mail too. The web site also has a live chat feature obtainable 24/7, you have got to|must you} want to get a response rapidly. All different cryptos besides Ethereum – minimum deposit of $20 to $100,000.

Post a Comment

Cron Design Studio: Dublin based web design & software development