 # Math.random() versus random();

Okay… Im missing something here:

If I use random(3), I get a value between 0 and 2, just the way it is supposed to be.

random() has been deprecated, so we really should not use it. Right? Knowing this, I said: "okay, I’ll use Math.random(), the new, more robust cousin of random().

If I use Math.round(3), my values are always less than 1.

I looked in the reference, and it says…

Description:

Method; returns n, where 0 <= n < 1.

I dont understand where the problem is… Do you guys know?
Oh, and I tried to round the number, so I only get either 0 or 1.

any thoughts?

(This message was left blank)

well… I got it to work, but is this the right way?

mynumber=Math.random()*2

that returns all kinds of decimal numbers, so I changed to

mynumber=Math.round(math.random()*2);

trace(mynumber) would give me a value equal to 0, 1, or 2

while we’re at it, which one is preffered?

mynumber=Math.round(math.random()*2);
trace(mynumber);

or

mynumber=Math.random()*2;
trace=Math.round(mynumber);

thx…

if you want whole numbers use random() but if you want say random rotation of a movieclip then you would use Math.random()*360 instead of random(360) it gives more possible rotations…

random() is supposedly “less random” than Math.random();

for most purposes, it won’t make a difference, do whatever you please.

if you are using Math.random(), and want whole numbers, you might consider using Math.floor or Math.ceil rather than Math.round as it will give you more even odds.

ie. Math.round(Math.random()*4) will give you a random number between 0 and 4, but 0 and 4 will only be returned half as often as 1, 2, or 3.

0 -.4999… (a spread of .5) will yeild 0, but .5 to 1.499… (a spread of 1) will yeild 1.

see the inequality?

Math.floor(Math.random()*4) will return a number between 0 and 3 at an even probablility.

go figure… you guys are great. Thanks for the help.