A Flash Developer Resource Site

1. ## random() vs math.random()

ok, hopefully someone can explain to me why

random(6)+1//which I understand is now depricated

has been exchanged with this:

Math.ceil(Math.random()*6+1)//hope I got that right

isn't the first one easier?

2. I think it was stupid that random() deprecated, which is why I continue to use the old 'random' with abandon. I am reasonably certain that it will never really go away, because it is just too darn convenient.

However, to play Devils advocate, the reason for the deprecation was probably as follows:

1. Math.random(), is more "object-oriented" - being part of the Math class. All the functions which aren't attached to a class prototype have been deprecated.

This is the anal-retentive object-oriented police at work. Sometimes consistency is good, but often in software engineering, there are a lot of little minds full of hobgoblins on the design team.

You might ask the question: Then how come Math.random() doesn't work the same way as random()? This leads us to #2.

2. Math.random(), which returns a floating point value (the same as RND() in BASIC), is more "flexible" than the integer-only variety. Since coordinates and other values in Flash also accept non-integer quantities, it is useful to be able to multiple Math.random() by a range, and get EVERYTHING in that range, and not just the integers. And of course, if you want the original random(n), you can always use Math.floor(Math.random()*n), which is the same thing. (NOT Math.ceil() mind you! -- see below)

Now, having played Devils advocate, I'll make my case for random().

In my opinion, if they were going to drop random(), they should have implemented the following three functions in the Math class (all of which I typically use in my C programs).

Math.frandom() - a floating point random that acts like the current Math.random()

Math.random() - an integer random that acts like the current random(). In 90% of the situations in which people use Math.random() (typically for selecting between a handful of random choices) they want integers. This is why I still use the old deprecated random() all the time.

Math.rseed(seed) - any language worth it's salt ALSO includes a method to seed the random number generator, so that you can make it reliably repeat the random sequence - VERY useful for testing, among other things. In fact, in most lower level languages, you have to explicitly use the seed function (with the clock) to 'randomize' the random number generator. Flash and Perl, being higher level languages do this automatically. It would still be nice to have the option.

Finally a note on using Math.ceil() instead of Math.floor() with random. I see this a lot.

As you know, random(n) returns a number from 0 thru n-1. Lots of folks like their random numbers to go from 1-n instead of 0-(n-1). I am not one of them, since by old habit I always tend to number my stuff starting from zero.
Arrays start zero, and so do I. For this reason, I can just use random(n) to choose something, instead of random(n)+1. I don't like having a lot of "+1"s in my code, so I've adjust my counting habits accordingly.

Folks who use Math.ceil(Math.random()*n) usually want a random number that starts at 1. They want something like random(n)+1.

The exact equivalent of

random(n)

is

Math.floor(random(n));

The exact equivalent of

random(n)+1

is:

Math.floor(Math.random()*n)+1

AND NOT

Math.ceil(Math.random()*n)

While the ceil variant has seemingly identical results, it is not exactly the same. While it almost always returns a number from 1 to n, there is a VERY MINISCULE chance that it will return zero. And it will most likely return zero on the day you are demonstrating your cool-ass random movie for a well-heeled client.

Why is this?

Because Math.random(), while it will never ever return 1, will occasionally (VERY occasionally) return zero. In other words:

0 >= Math.random() < 1

And Math.ceil(0*n) is zero.

You have been warned.

- jbum

3. thanks, as always for the IN DEPTH response but I was wondering, if I use random(6)+1 I get 1-6 as my results, right? so if I use Math.random()*6+1, I'll get 0 - 5.99999....whatever, so if I use Math.floor, the numbers that should go from 1-6 will be 0-5, no...I mean to duplicate random(6)+1 with the new syntax, would I not want Math.ceil? so that my 5.9999's go up to 6?...I hope I'm not TOTALLY missing what you are saying, it might be over my head, not the first time...

**edit..p.s. if I'm waaaay off, I've only been at this AS thing for about 5 months, and most of that was with flash 5...just got MX04 in february....what a jump from flash 5, man!!

4. No problem. Check this out:

Code:
```this is not code, just want the alignment

random(6)                     => 0 - 5

Math.random()                 => 0 - .9999

Math.random()*6               => 0 - 5.9999

Math.floor(Math.random()*6)   => 0 - 5 (integers)

Math.floor(Math.random()*6)+1 => 1 - 6 (integers)
Note that the +1 happens after the floor.

Math.ceil(Math.random())      => 0 - 1 (integers)
with a very-very low chance of getting zero

Math.ceil(Math.random()*6)    => 0 - 6 (integers)
with a very-very low chance of getting zero```
I've also been at this Flash thing a relatively brief time (just a few months now). The difference is that I've been programming in *other* languages a long-long time (since the early 80s). It gets easier to pick up new languages after you have a couple of them under your belt.

It's also been an advantage to not have to work in Flash 4 or Flash 5. That's a lot of bad baggage to have to carry around!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap