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

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

Unknown 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

Unknown 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

Unknown 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


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...

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

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

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

Lalisa Lisa 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

Pankaj Singh said...

Superb article and valuable information because it’s really helpful for me, Thank you so much for share this wonderful information with us.
Jeans Manufacturers in Delhi

Just Info said...

Such a very superb article, Very interesting to read this article, I would like to thank you for useful had made for writing this awesome article.
Top Commercial Vehicle Painters

deepak ogen said...

Awesome article, Very interesting to read this article, I would like to thank you for useful had made for writing this superb article share with us.
Web design company

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

Kala Kutir said...

Good article, Very interesting to read this article, I would like to thank you for writing this helpful article share with us.
Lifestyle Magazine

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.

Juliana Kho said...

2
3
4
5
6 osg777 download
7
8

Bukalapak said...

This article is interesting and useful

Keunggulan Khasiat Dan Manfaat Walatra Gamat Emas Kapsul
Obat Paling Manjur Untuk ISK
Obat Herbal Untuk Penyakit Encok
Bahaya Keringat Dingin
Obat Herbal Radang Panggul
Cara Mengobati Penyakit Kusta
Obat Herbal Kalazion

Bukalapak said...

This article is interesting and useful

Keunggulan Khasiat Dan Manfaat Walatra Gamat Emas Kapsul
Obat Paling Manjur Untuk ISK
Obat Herbal Untuk Penyakit Encok
Bahaya Keringat Dingin
Obat Herbal Radang Panggul
Cara Mengobati Penyakit Kusta
Obat Herbal Kalazion

Cara Menghaluskan Wajah said...

Thank you very helpful article. allow me to join to share health articles with your article hopefully useful.

Obat Keloid Terampuh
Obat Kaki Bengkak Alami
Cara Mengobati Kaki Gajah
Obat Herbal Untuk Penyakit Kolera
Cara Cepat Untuk Menghilangkan Panu
Cara Mengobati Tumor Otak

Mobile App Development in Noida said...

Thanks for the article may be useful for everything

Unknown said...

Rice Bags Manufacturers
Pouch Manufacturers
wall putty bag manufacturers
Lyrics with music

Unknown said...

we have provide the best ppc service.
ppc company in gurgaon
website designing company in Gurgaon
PPC company in Noida
seo company in gurgaon
PPC company in Mumbai
PPC company in Chandigarh

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

Unknown said...

gmp certification in delhi
ISO Certification in Noida
iso certification in delhi
ce certification in delhi
iso 14001 certification in delhi
iso 22000 certification in delhi
iso consultant in delhi
rohs certification in delhi
website designing services
seo serivces in delhi

cara menggugurkan hamil said...

now present in your city
1. manfaat kurma untuk persalinan
2. manfaat buah nanas
3. aktivitas penyebab keguguran
4. apakah usg berbahaya
5. penyebab telat haid
6. cara melancarkan haid
7. cara mengatasi keputihan
8. tanda tanda menopause

Pradeep Appslure said...

Mobile app development company in mumbai

prashant said...

Nice Post!


Dog breeds in India
Places to visit in Bangalore
Mysore Palace
National Parks in India
Mount Abu

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

anonymus said...

shopeetoto
shopeetoto
shopeetoto
shopeetoto
shopeetoto
shopeetoto
shopeetoto
shopeetoto

Gusion said...

pelangiqq
pelangiqq
pelangiqq
pelangiqq
pelangiqq
pelangiqq
pelangiqq
pelangiqq

anonymus said...

http://62.171.145.61/anugerahtoto/
Togel Online
Poker Online
bandarq
Bandar Kometqq
capsa online
agen online qq
agen poker

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

Rajan Mhatre said...

I have study your article, it is very informative and beneficial for me. I recognize the valuable statistics you offer in your articles. Thanks for posting it and is.
Visit us for Notebook Kraft Pad.

Unknown said...

Thanks for sharing this blog.

Bis Consultant 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

Max Russell said...

I am very happy to read this. This is the kind of manual that needs to be given and not the random misinformation that’s at the other blogs. I appreciate your sharing this best blog. Android app development companies. You can email us at: sales@appsquadz.com or Mobile Number: +1- 570-234-9288.

Qater said...

here you can get more info about Allama Iqbal open university result data and student ID process, application tracking, BA Admission
Allama iqbal open university

raza said...

Hi I am from Join Pak Navy I and I really like your work thanks for this information and keep it up you are doing good.

Health Lobby said...

Case Search online at Sindh High Court can provide you full details about your case at Sind High Court. Check the hearing date online. On Just one click you can find all the details through Case Search Sind High Court.

yanmaneee said...

kobe
supreme
pg 4
pandora
kenzo clothing
hermes handbags
supreme hoodie
golden goose
golden goose sneakers
stone island sale

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

Pradeep Appslure said...

Award-winning mobile app development agency. Our android application development company builds custom mobile apps for 7+years. Over 600+ clients served.
mobile app development agency

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

Astrid said...

Thanks a lot for the nice and amazing website. https://southbeachhairsalon.com/

Shivani thakur said...

Brilliant post, checkout this as well:- psychology tutors

for a said...

Great Article Image Processing Projects Deep Learning Projects for Final Year JavaScript Training in Chennai JavaScript Training in Chennai The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training Project Centers in Chennai

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