i have never figured out how to do this
any help aprreciated
Hey Kaotic,
Just a random number? Here’s how:
variable = low_num + Math.random() * high_num;
Of course, replace low_num and high_num with a numerical equivalent.
Cheers!
Kirupa :q:
Ok let’s say you want an integer from 12 to 49 (just an example) assigned to variable myRandomNum:
myRandomNum = Math.round(Math.random()*(49-12)+12);
That’s it. Math.round(realNum) rounds off realNum to the nearest integer. Math.random() returns a random real (floating point) number from 0 to 1. So you just modify the results to get the range you want. If you work through it in your head you’ll get it. Just try two cases: 1) Math.random() returns 0. 2) Math.random() returns 1. Then just modify the result to get the range you want. Here are other examples for your interest (I haven’t simplified any of the calculations in order for you to see where I am getting the numbers from…you can always simplify the calculations, of course, but if you write them the way I have it makes changing things easy, as well as making the line of code easier to read):
Random real number to two decimal places between 5.00 and 18.00:
myRandomNum = Math.round((Math.random()*(18.00-5.00)+5.00)*100)/100;
Random integer from -23 to 23:
myRandomNum = Math.round(Math.random()*(23-(-23))+(-23));
Random real number between 3 and 12:
myRandomNum = Math.random()*(12-3)+3;
Hope that helps.
-Al
::EDIT:: ■■■■…Kirupa beat me.
thank you both
*Originally posted by alethos *
**::EDIT:: damn…Kirupa beat me.**
Yep but he made a mistake :beam:
You are right!
what mistake is that
Well, Al explained it more or less. Basically, you want a random number between low_num and high_num, right?
So Math.random() * high_num;
will give you a random number between 0 and high_num,
and low_num + Math.random() * high_num;
will give you a random number between low_num and high_num+low_num.
That’s why you have to substract low_num to high_num in the multiplication.
pom :moustache
thank you
Math.floor should be used with Math.random. Math.random generates a random float between 0 inclusive and 1 exclusive. Multiplying this by a value gives you a number between 0 and that number (though not ever reaching that number). If rounded with round, the values at the endpoints of that range, both 0 and the multiplier, would have 1/2 the chance of being hit compared to all other numbers. for example:
2 is gotten from 1.5 - 2.5, a range of 1
1 is from 0.5 - 1.5
0 is -0.5 - 0.5 <-- but a negative value is never gotten from the random call so 0 is less likely to be hit.
What floor does is always rounds down and spreads an equal chance to all numbers being hit. The result of using
Math.floor(Math.random()*multiplier) is the same as the original flash random function, giving a number between 0 and upto but not including the multiplier (or number supplied) - which is the way it should be. For a random range, you can use this:
Math.randomRange = function(min, max){
return Math.floor(Math.random()*(max-min+1)) + min
}
and just use Math.randomRange(12,49) and youll get a number between 12 and 49 inclusive