PDA

View Full Version : Fruit machine tutorial!



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

bbwd
December 10th, 2004, 01:28 PM
Math.random(); is outdated. Sure it ensures backwards compatibility with older players, but most people have Flash Player 7. Use random(); instead.

E.g:

//frame 1
random(4);

Ben H
December 10th, 2004, 03:27 PM
75% of website viewers have Flash player 6...

Sammo
December 12th, 2004, 06:25 PM
Are you not going to complete this tutorial then om3ga?

I hope you will...

Ben H
December 13th, 2004, 04:14 PM
I will complete it, now that I'm off school ;D

JoMan
December 13th, 2004, 06:04 PM
I know this doesnt have anything to do with the forum, but om3ga, your phooter is very nostalgic to me :3.

nipi
December 13th, 2004, 06:13 PM
all about the christmas is a bit nostalgic

lostinbeta
December 13th, 2004, 06:20 PM
Math.random(); is outdated. Sure it ensures backwards compatibility with older players, but most people have Flash Player 7. Use random(); instead.

E.g:

//frame 1
random(4);


Actually random() is the outdated one. It's been deprecated since Flash 5 I believe.

Math.random() is the new form for random numbers.

Which I don't understand because random runs faster than Math.random() and although not noticeable on standard use (the difference is a fraction), code that relies on a lot of mathematical algorithms that involve random numbers can actually have increased efficiency when using the deprecated random() over Math.random().

I'm just glad the new versions still support random().



@om3ga: You know kirupa.com has a random number tutorial right?

JoMan
December 14th, 2004, 11:35 AM
all about the christmas is a bit nostalgic
True :).

bbwd
December 14th, 2004, 12:51 PM
Which I don't understand because random runs faster than Math.random() and although not noticeable on standard use (the difference is a fraction), code that relies on a lot of mathematical algorithms that involve random numbers can actually have increased efficiency when using the deprecated random() over Math.random().

I didn't know Math.random() was the newest one... Thanks. But you'd expect random() to operate more slowly if they [Macromedia] replaced it with another function... Strange.

Sammo
December 14th, 2004, 02:00 PM
If Math.random() runs slower, and Macromedia replace it for random(), there must be advantages for Math.random()....

Anyone know what they are?

lostinbeta
December 14th, 2004, 02:54 PM
I honestly have no clue what the advantages are. I suppose in some ways, it's support for decimal numbers can be used as a plus (random() produces whole numbers without a need to Math.floor(), Math.round(), or Math.ceil() them) in certain situations.... maybe. And I don't know if the process behind how Math.random() works is better at being more random than random() is.

Just some theories, I don't feel like researching it at the moment... I'm lazy ;)

And like I said, the difference in speed is a tiny fraction that is really only something to be concerned about in advanced applications that involve a lot of mathematical functions and random numbers. Standard use won't really show a performance difference.

Ben H
December 15th, 2004, 04:41 PM
lol

smoothhabitat
December 16th, 2004, 03:15 AM
I can see why they switched the syntax to Math.random(), in keeping with the movement towards classes and such, it seems like a logical grouping. But weird about the slow down.