Ben H
December 9th, 2004, 12:44 PM
Hey!
After a thread I helped someone in to do with dice, I was inspired to make a tutorial on creating a flash fruit machine.
So here we go...
Random Numbers
The first thing you need to know about is random numbers.
The first function you need to know about is Math.random(). It creates a random number from 0 to 1. Now I know that you're thinking that an endless stream of decimals is not much use... So what about Math.random() * 10? That creates a number from 0-10! But it still has a random stream of decimals http://kirupaforum.com/forums/images/smilies/depressed.gif. So we need to round it to the nearest whole number! So it's Math.round() to the rescue! Math.round rounds to the nearest whole number. So now we have Math.round(Math.random * 10)... What if we want a number from 1-10? We use Math.ceil, it always rounds up, so 0 is impossible! The opposite of Math.ceil is Math.floor, so you will get a random number from 0-9.
Anyhoo, here is a summary of this part:
Use Math.random() for a random number from 0-1
Multiply it by 10 for a random number from 0-10
Use Math.round(), Math.ceil and Math.floor to round numbers.
That's it for part one. Mess around with it, because next lesson we will make the screens with a random number!
Enjoy!
-om3ga
After a thread I helped someone in to do with dice, I was inspired to make a tutorial on creating a flash fruit machine.
So here we go...
Random Numbers
The first thing you need to know about is random numbers.
The first function you need to know about is Math.random(). It creates a random number from 0 to 1. Now I know that you're thinking that an endless stream of decimals is not much use... So what about Math.random() * 10? That creates a number from 0-10! But it still has a random stream of decimals http://kirupaforum.com/forums/images/smilies/depressed.gif. So we need to round it to the nearest whole number! So it's Math.round() to the rescue! Math.round rounds to the nearest whole number. So now we have Math.round(Math.random * 10)... What if we want a number from 1-10? We use Math.ceil, it always rounds up, so 0 is impossible! The opposite of Math.ceil is Math.floor, so you will get a random number from 0-9.
Anyhoo, here is a summary of this part:
Use Math.random() for a random number from 0-1
Multiply it by 10 for a random number from 0-10
Use Math.round(), Math.ceil and Math.floor to round numbers.
That's it for part one. Mess around with it, because next lesson we will make the screens with a random number!
Enjoy!
-om3ga