Scriptplayground Network

Subscribe to Tutorial Feed

Flash and PHP Bible

The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area.

The Flash and PHP Bible has a forum for quick support.

Scriptplayground » tutorials » as » Generate random number in ActionScript 3

Generate random number in ActionScript 3

Custom function to generate a random number, using a low to high sampling.

This is not so much a tutorial as its a code snippet with example.

/** 
* Generate a random number
* @return Random Number
* @error throws Error if low or high is not provided
*/  
function randomNumber(low:Number=NaN, high:Number=NaN):Number
{
  var low:Number = low;
  var high:Number = high;

  if(isNaN(low))
  {
    throw new Error("low must be defined");
  }
  if(isNaN(high))
  {
    throw new Error("high must be defined");
  }

  return Math.round(Math.random() * (high - low)) + low;
}

Here is how to call the function, just a simple usage.. this can be used for any number of things. Pass in an array length, age range or coordinates.. its up to you

trace("Random Number : " +randomNumber(3, 10));
| Print It |  Follow Scriptplayground on Twitter (@scriptplay)

Comments: Generate random number in ActionScript 3

 jacob  Sat Dec 1, 2007 8:34 am  
why do you set the default values to NaN and throw runtime errors? Doesn't it make more sense to not set default values or make the default between 0 and 100?
 mkeefe  Sun Dec 2, 2007 11:20 pm  
I would have done that except its intended use was random key in an array. However, I do see your point and will make the necessary revision showing both options.
 SimonClark  Mon Mar 3, 2008 12:01 pm  
Beware!!!!

This will not produce a truly random spread of numbers. The highest number and the lowest number will get picked 1/2 as frequently as any other number. Use:

Math.floor(Math.random() * (1+high-low)) + low;
 Dick  Sat Oct 11, 2008 7:35 pm  
"Beware!!!!

This will not produce a truly random spread of numbers. The highest number and the lowest number will get picked 1/2 as frequently as any other number. Use:

Math.floor(Math.random() * (1+high-low)) + low; "


is this true? I really don't see how that is true, but i'm just a beginner with as3. can anybody clarify this?
 Dick  Sat Oct 11, 2008 8:48 pm  
Ahh... I must be blind. I see now what you're saying.. thanks for the tip Simon.
 vamapaull  Tue Nov 4, 2008 10:38 am  
great code
very useful
 vamapaull  Tue Nov 4, 2008 10:39 am  
Thank you!
 Nicholas Polet  Mon Dec 29, 2008 7:32 am  
Nice function here, I wrote a similar one in as2 but havent had a chance to convert it to as3, now I dont need to, plus yours is much better.

Thanks for this one
 mkeefe  Sat Jan 10, 2009 11:57 am  
@Nicholas - No problem, glad it worked for you.
 carl  Wed Aug 26, 2009 11:48 pm  
Thanks for sharing this! It sure did save me a lot of time.
 carl  Wed Aug 26, 2009 11:48 pm  
Thanks for sharing this! It sure did save me a lot of time.
 Waqas  Mon Nov 9, 2009 3:14 am  
Thankyou man ... That was a nice script
 PHANTOMAXWEL  Fri Dec 11, 2009 2:31 pm  
This is wrong!
for example if:
low=2
hight=53

Math.round(Math.random() * (high - low)) + low;
will return 54!!!!!!!!!!!!!

You must use:
Math.floor(Math.random() * (high - low)) + low;
 Mendim  Sun Dec 27, 2009 10:56 pm  
@PHANTOMAXWEL

No, you are wrong. It works perfectly. I tested it:

for (var i:uint = 0; i < 1000; i++) {
trace(Custom.rand(45,58));
}

I Ctrl + F'd for any 44's or 59's. I found none.
 Mike  Tue Jan 12, 2010 5:45 pm  
Is there any way to avoid repetition?
 M. P. Junior  Tue Feb 16, 2010 3:01 pm  
Hi, i have a solution for random number avoiding repetition.

var ArrayCriaBolas:Array = new Array();
for(var u:uint = 1; u <= 99; u++){
ArrayCriaBolas.push(u);
}
// o Trace abaixo imprime a verificação se o aray esta puxando os numeros certos
//trace("A C B = " + ArrayCriaBolas);

// o While abaixo embaralha os números dentro de novo Array
var ArraySorteiaBolas:Array = new Array(); /* ESSE SERIA O ARRAY DAS BOLAS SORTEADAS */
while (ArrayCriaBolas.length >0) {
var w:int = Math.floor(Math.random()*ArrayCriaBolas.length);
ArraySorteiaBolas.push(ArrayCriaBolas[w]);
ArrayCriaBolas.splice(w,1);
}
 M. P. Junior  Tue Feb 16, 2010 3:03 pm  
//verificação do array total
//trace("A S B = " + ArraySorteiaBolas);
Add a comment
Name:
Website:
Comment:
Please note: Offensive comments, flaming and spamming is not permitted on this site and your comment will be deleted immediately.

HTML is not allowed.

Please provide all comments in English so that others can help you. A common helper in this is to use an online translator.

As a security measure your ip will be recorded.
 
Anti-Robot Check:

Enter the key you see above.

What is this?: This extra test has been added due to the recent explosion of spam.
 
Google