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 )
      ret = round( Math.random()*(to-from)+from, decimalPlaces )

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


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

Unknown said...

canada goose
coach factory outlet
coach outlet
ray ban sunglasses
fit flops
canada goose jackets
kate spade outlet
cheap oakleys
birkenstock sandals

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

dewatampan said...

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

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

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.

Getting Payment Gateways in Europe said...

Dive into a seamless financial frontier with the credit card processing ! Experience swift transactions, unrivaled security, and unparalleled convenience with #WebPays. This cutting-edge solution redefines the way you handle payments, unlocking a new era of efficiency. Say goodbye to hassles and hello to a future where every transaction is a breeze.

ITIO Innovex said...

Discover seamless transactions with the White Label Payment Gateway at ITIO . Tailored solutions empower your brand while ensuring security and flexibility. Join the revolution in payment processing, offering customizable interfaces and advanced features to enhance your business's digital transactions.

Post a Comment

Cron Design Studio: Dublin based web design & software development