# Math.random vs. random()

what would be the advantages of using either one of these over the other? it seems to me that if I wanted values between 1 and 100 I would rather use random(100) as it could return ANY integer value from 0 to 100… if I use Math.random()100 I can only get these values:
0.0
100 = 0
0.1100 = 10
0.2
100 = 20
0.3100 = 30
0.4
100 = 40 …etc. ie I can only get 10 different values!

Is this correct, is there a way around this??? any info would be great!! Thanks
Peace

That sounds kind of odd to me. Try this (add it to any movie clip)…

``````
onClipEvent (enterFrame) {
trace(Math.random()*100);
}

``````

Watch as the numbers fly down the output window. All random integers from 0-100. Of course these are decimals, very sloppy.

For nice clean numbers use this…

``````
onClipEvent (enterFrame) {
trace(Math.round(Math.random()*100));
}

``````

A list of whole numbers will constantly be outputted to the output window.

Notice that there are definitely more than 10 possibly outcomes. There are 100 possible outcomes.

ahhh yes, I see, I was unsure how many places back the number given went… that must be the problem I am having - that is my MCs are random, but… I guess not THAT random…hmmmm

What problem are you having?

Math.random(); produces a number between 0 and 1 but it returns that number as a three digit integer so you will get any value you like

Math.random(); could therefore come up with this ‘.234’

which would be 23.4 if you multiplied it by 100. Rounded it would be 23.

For the record, the old ‘random();’ method has been depreciated… so there is a good chance that the next version of Flash will not allow it at all as a method. So get used to using the ‘Math’ methods.

In addition… the math method is more random. I know that sounds weird. How can anything be more random than another random number… but that’s how computers work.

Why was Math.round() depreciated? I think it is a handy tool for making whole numbers instead of the standard decimal number that regular Math.random() gives you.

Is there another way to doing this without using Math.round()?

sorry… typo. I meant that the old ‘random’ was depreciated. I’ll change it in my post above.

thanks for all the info guys! BTW lostinbeta: my troubles were not what I had thought… another simple number error that was excluding most of the, uh “randomness” of my MC… once again one of those darn little things. Everything is working VERY well now.
Peace

A few things: Math.random returns more than 3 digits, Upu.
And, as most of Flash 4 functions, it is faster than Math.random().
pom :asian:

When I used Math.random()*100 I got numbers like 45.5251852050424. That is 15 digits, a very big number. I am glad to see that round() wasn’t depreciated, because I tend to use that a lot. I am not one for decimal numbers.

RYALL: Glad to see you got everything up and working:)

FYI, my friends… random and round were deprecated not depreciated.

There is also a problem with just using Math(random) because it will provide random numbers BETWEEN, but not including, the start and end numbers.

The proper format, in my opinion, should be:

min=1;
max=100;

Math.floor(Math.random()*((max-min)+1))+min;

This will cover all numbers from 1 to 100, without exclusions.

I vaguely remember one other reason why you should use Math.floor and Math.random… it had to do with certain numbers showing up more often than others. I remember it was Supra who is instructed me on this phenomenom.

Ahhhhhh - thanks Iammontoya! That helps me out, I was using quite a hack to set the range of values I needed, thanks this way is a lot cleaner - and you say it comes up different numbers more often? interesting, I wonder why. Works for me!

PS did you ever make any headway on that .fla I sent you? The one with the mouse trailer (reading: International Importers, Inc.) and the preloader conflict?

Deprecated…Depreciated…same diff:P haha

Thanks for the new info. I didn’t know Math.random() only did the numbers BETWEEN your start and end numbers. That is definitely good to know. I used regular Math.random() for a random frame effect, but It wouldn’t play the last frame, definitely explains why.

It is also good to know about the Math.floor() thing.

Very edumacational…or is that educational?

you crack me up LostinBeta…

Ryall… I definitely have you on my list, sorry it’s taken so long.

I wont get on a soapbox about depreciated/deprecated hahahahahahahaha…

No problem, thanks for helping out!

Peace

I sense sarcasm Montoya. I hope I didn’t offend you. I was merely trying to turn our honest mistake into a joke. It was nothing against you.

sorry guys… me bad. Can’t be right all the time. Thanks for the clarifications.

I think that we’ve really detailed the differences here.

Hey Inigo, are you sure rounding was deprecated? I have a doubt (and I don’t see why they would do that, since there’s no replacement function :)).

pom :asian:

heheheh… guys… I’m never THAT serious. It was all in jest… we’re all Flash brotha’s and sista’s. Right?

If anything ever comes out sounding serious from me… it probably isn’t. I’m 98.9 percent happy-go-lucky! heheheheheheh

Ilyas… almost every math function that did not start with “Math.” was deprecated in favor of things like

Math.floor
Math.random
Math.Round
Math.cos

etc…

Oh, I always start everything with Math. I thought you meant that it was gone for good. Phew.

Hey Montoya, what is the difference between Math.floor and Math.ceil? To me that looks like floor and ceiling, and I have no clue what the difference is.

Does it have anything to do with it choosing higher or lower numbers more often?