# Math.random() versus random();

**URL:** <https://forum.kirupa.com/t/math-random-versus-random/1853>\
**Category:** Uncategorized\
**Created:** [May 10, 2002, 6:50pm UTC](https://forum.kirupa.com/t/math-random-versus-random/1853 "2002-05-10T18:50:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 10, 2002, 6:50pm UTC](https://forum.kirupa.com/t/math-random-versus-random/1853/1 "2002-05-10T18:50:19Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 10, 2002, 6:51pm UTC](https://forum.kirupa.com/t/math-random-versus-random/1853/2 "2002-05-10T18:51:43Z")

</div>

(This message was left blank)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 10, 2002, 7:05pm UTC](https://forum.kirupa.com/t/math-random-versus-random/1853/3 "2002-05-10T19:05:04Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 10, 2002, 7:50pm UTC](https://forum.kirupa.com/t/math-random-versus-random/1853/4 "2002-05-10T19:50:32Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 11, 2002, 12:11am UTC](https://forum.kirupa.com/t/math-random-versus-random/1853/5 "2002-05-11T00:11:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 11, 2002, 1:41am UTC](https://forum.kirupa.com/t/math-random-versus-random/1853/6 "2002-05-11T01:41:13Z")

</div>

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