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.
Custom function to generate a random number, using a low to high sampling.
This code snippet focuses on the topic of random numbers. Below is the original (3 years old) method and a new method added just now.
/**
* Generates a truly "random" number
* @return Random Number
*/
function randomNumber(low:Number=0, high:Number=1):Number
{
return Math.floor(Math.random() * (1+high-low)) + low;
}
/**
* 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));
|
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); |
|
renu Thu Mar 25, 2010 2:32 am
can I generate non repeating random numbers between 1 to 500 in flash cs3.
|
|
Mike Thu Apr 8, 2010 10:31 am
If they are non-repeating, doesn't that by definition mean that they're NOT random?
Generating truly random numbers is different than pulling items from an array by randomly selecting their indexes without repetition. |
|
david Fri Apr 16, 2010 1:57 pm
Mike, the issue is not the definition of "random" (you are right on that), but a random number from a selection that excludes previously used ones...
|
|
Marco Mon Jul 19, 2010 9:26 pm
DO NOT USE THIS FORMULA! It will not give you a truly random result. Use this instead:
Math.floor(Math.random()*(1+High-Low))+Low; It's a shame this is the first result when I search this topic on Google, and it's wrong. How do we get mkeefe to update to the correct formula? |
|
Treblah Sun Jul 25, 2010 12:50 pm
If you don't like the results that are produced with this function and you know how to do it better than do it your way don't sit here and rag on mkeefe, this is probably close enough to random for him, I know it worked fine for what I needed to do. Thanks for the post mkeefe.
|
|
mkeefe Tue Jul 27, 2010 8:55 am
@Marco - While I appreciate your input, the reason this tutorial is #1 is because others have used/enjoyed it.
As an aside I have updated the tutorial to reflect comments on this site, thanks all. |
©2004 - 2010 scriptplayground | Privacy Policy | Legal
Validate Site: XHTML CSS | Designed by: Matthew Keefe of mkeefeDESIGN