# Math random and floor

**URL:** <https://forum.kirupa.com/t/math-random-and-floor/260519>\
**Category:** Uncategorized\
**Created:** [May 16, 2008, 12:16pm UTC](https://forum.kirupa.com/t/math-random-and-floor/260519 "2008-05-16T12:16:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kv79](https://avatars.discourse-cdn.com/v4/letter/k/47e85d/32.png) [@kv79](https://forum.kirupa.com/u/kv79)\
**Post date:** [May 16, 2008, 12:16pm UTC](https://forum.kirupa.com/t/math-random-and-floor/260519/1 "2008-05-16T12:16:05Z")

</div>

Hi all

```auto

rects*.graphics.beginFill(Math.floor(Math.random( )*0xFFFFFF), 1);

```

I can’t find , what floor and Math.random( ) do to this code.I have a reference .

Thanks advance .

---

<div class="post-metadata">

**Author:** ![skineh](https://avatars.discourse-cdn.com/v4/letter/s/cab0a1/32.png) [@skineh](https://forum.kirupa.com/u/skineh)\
**Post date:** [May 16, 2008, 1:05pm UTC](https://forum.kirupa.com/t/math-random-and-floor/260519/2 "2008-05-16T13:05:02Z")

</div>

Hey there, kv79. Math.floor() and Math.random() are being used here to create a random color to use as the fill color. They’re done in two steps:

```auto
Math.random() * 0xFFFFFF

```

This creates a random color/number. It does this by calling Math.random(), which will return a number value from 0-1. That number is then multiplied by 0xFFFFFF, making the possible value range 0x000000 to 0xFFFFFF.

Because 0xFFFFFF is multiplied by a decimal value, it’s possible the end result isn’t a valid color (it’d be like trying to say 0xFFFFFF.667) Hexidecimal values are integral, so to make sure of this, Math.floor() is used. Math.floor() takes a value (in this case the random color just created) and rounds it down to the nearest integer.

---

<div class="post-metadata">

**Author:** ![kv79](https://avatars.discourse-cdn.com/v4/letter/k/47e85d/32.png) [@kv79](https://forum.kirupa.com/u/kv79)\
**Post date:** [May 16, 2008, 4:17pm UTC](https://forum.kirupa.com/t/math-random-and-floor/260519/3 "2008-05-16T16:17:40Z")

</div>

How can Hexidecimal number can be multiplied with number between 0-1 ?  
Decimal \* Hexidecimal =\> abstract .

---

<div class="post-metadata">

**Author:** ![Felixz](https://avatars.discourse-cdn.com/v4/letter/f/ecb155/32.png) [@Felixz](https://forum.kirupa.com/u/Felixz)\
**Post date:** [May 16, 2008, 4:40pm UTC](https://forum.kirupa.com/t/math-random-and-floor/260519/4 "2008-05-16T16:40:21Z")

</div>

well all numbers are stored binary, so u don’t have to worry

---

<div class="post-metadata">

**Author:** ![skineh](https://avatars.discourse-cdn.com/v4/letter/s/cab0a1/32.png) [@skineh](https://forum.kirupa.com/u/skineh)\
**Post date:** [May 16, 2008, 5:38pm UTC](https://forum.kirupa.com/t/math-random-and-floor/260519/5 "2008-05-16T17:38:56Z")

</div>

Yeah, as far as Flash is concerned, 0x00001B is the same thing as saying 27.

---

<div class="post-metadata">

**Author:** ![superpeppe](https://avatars.discourse-cdn.com/v4/letter/s/a183cd/32.png) [@superpeppe](https://forum.kirupa.com/u/superpeppe)\
**Post date:** [May 16, 2008, 6:05pm UTC](https://forum.kirupa.com/t/math-random-and-floor/260519/6 "2008-05-16T18:05:52Z")

</div>

Instead of using Math.floor(), use the uint(), it’s faster, and Math floor actually produces a Number, not an Integer. Because of accuracy problems with floating values, Math.floor(4.7458) could return 4.000000008758

Therefore use uint(), it just cuts away everything behind the coma.

```auto
uint(Math.random()*0xFFFFFF)

```

But actually I think that it’s unnecessary to round it, flash does that by itself, because beginFill only takes uint’s (from the Flash language reference):

```auto
beginFill(color:uint, alpha:Number = 1.0)

```

So to sum up, your final code could be:

```auto
rects*.graphics.beginFill(Math.random()*0xFFFFFF);

```

But the other suggestions will work just fine, but this is faster and easier(for further coding)

By the way, avoid setting alpha to 1, it’s the default value:thumb2:

---

<div class="post-metadata">

**Author:** ![Anogar](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anogar](https://forum.kirupa.com/u/Anogar)\
**Post date:** [May 16, 2008, 6:25pm UTC](https://forum.kirupa.com/t/math-random-and-floor/260519/7 "2008-05-16T18:25:07Z")

</div>

It’s a **uint** typed parameter anyway, the Math.floor() is unnecessary.

---

<div class="post-metadata">

**Author:** ![kv79](https://avatars.discourse-cdn.com/v4/letter/k/47e85d/32.png) [@kv79](https://forum.kirupa.com/u/kv79)\
**Post date:** [May 16, 2008, 8:44pm UTC](https://forum.kirupa.com/t/math-random-and-floor/260519/8 "2008-05-16T20:44:27Z")

</div>

Thanks to all .
