Flash Components      Flash Menu      Flash Gallery      Flash Slideshow      FLV Player      Flash Form      MP3 Player      PhotoFlow      Flash CMS      3D Wall      Flash Scroller

Flash / AS

Silverlight

WPF

ASP.net / PHP

Photoshop

Forums

Blog

About

 


FlashComponents
  Galleries
  Slideshows
  Menus
  Design & Effects
  Audio & Video
  User Interface
  Templates

 

 

 

 

 
Random Numbers
       by kirupa chinnathambi

In programming, it is nice to be able to create numbers that occur unexpectedly.  To create numbers that occur with no set pattern, one would need to use random numbers. To create a random number, you would use the following function:

Math.random()

While the random function is quite limited when used alone, you can configure it to generate numbers that fall within a defined range. More than likely, for all of your animations, you would want to set a range criteria for the random numbers.

To configure a number to randomly fall within a range, you would use the following format:

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

The text high refers the highest possible value the random number function will generate. Likewise, low produces the least possible value for a random number. You should note that you are not limited to using only positive values. You can set low to be a negative number, and the range of the random numbers will still fall within high and low.

For example, let's look at the following line of code:

Math.floor(Math.random() * (70 - 7)) + 7;

A random number between 7 and 63 will be displayed. If you look at the line of code, you will see the function Math.floor() added prior to the Math.random() function.

When a random number is defined, it is usually not a nice, round number. The answer will have numerous digits following a decimal point such as 5.1403533. In order to round the number into a nice, solid integer, Math.floor() is used.

Math.floor() takes a number and rounds it down to the closest integer less than or equal to the number in question. For example, Math.floor(5.1403533) will yield 5. You can also use Math.round(), but that will be saved for another time.

Many of the tutorials in this site will use the Math.random() function, so I hope this little tip helps you understand this little facet of Flash programming lingo.

Thanks to Scootman on the forums for noticing a blatant error I made while writing the first version of this tutorial!

Happy Flashing!
Kirupa Chinnathambi

 

Hope this tutorial helped. If you have any questions, please post them on the forums at http://www.kirupa.com/forum/

 

kirupa.com's fast and reliable hosting provided by Media Temple. flash components
The Text Animation Component for Flash CS3
Check out the great, high-quality flash extensions. Buy or sell stock flash, video, audio and fonts for as little as 50 cents at FlashDen.
Check out our high quality vector-based design packs! Flash Effect Components
flash menus, buttons and components Digicrafts Components
The best flash components ever! Entheos Flash Website Templates
Buy and sell FLAs at Ultrashock! Upload, publish, deliver. Secure hosting for your professional or academic video, presentations & more. Screencast.com
Purchase & Download Flash Components flash components
Learn how to advertise on kirupa.com