# A Tip When Using Math.random in AS3

**URL:** <https://forum.kirupa.com/t/a-tip-when-using-math-random-in-as3/634359>\
**Category:** flash\
**Created:** [March 19, 2016, 4:16am UTC](https://forum.kirupa.com/t/a-tip-when-using-math-random-in-as3/634359 "2016-03-19T04:16:33Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Twisted4000](https://avatars.discourse-cdn.com/v4/letter/t/f08c70/32.png) [@Twisted4000](https://forum.kirupa.com/u/Twisted4000)\
**Post date:** [March 19, 2016, 4:16am UTC](https://forum.kirupa.com/t/a-tip-when-using-math-random-in-as3/634359/1 "2016-03-19T04:16:33Z")

</div>

So I found out something interesting when using Math.random in AS3: when you use Math.round, which is basically the best, most unbiased way to try to get whole numbers, the minimum and maximum outcomes will actually have LESS of a chance than every other number. So for instance, if you were trying to generate a number from 1-10, 2-9 would have a higher chance (11.25% each) of getting generated than 1 and 10 (5.62% each) instead of all of them actually being at 10%.

Now why is this? Well, since the minimum number is 1, the only possible way to get a 1 is to get anywhere from 1 to 1.5, while getting another random number like, say, 6, you can get anywhere from 5.5 to 6.5, which, as you can see, is double the leeway of getting a 1 (and the same exact thing obviously applies to 10, since you have to get a number anywhere from 9.5 to 10).

So this is just something to keep in mind when using the RNG in AS3. I did find a workaround you can use to get a true, unbiased percent for whatever you’re doing, however: if you were trying to get a number from 1 to 10, code it as 0 to 11 instead. 0 and 11 will have a lower chance of getting picked than the other numbers, but if they DO get picked, you can make the randomizer run again. So for example:

var rng = Math.round(Math.random() \* 11);  
if (rng == 0)  
{  
rng = Math.round(Math.random() \* 11);  
}  
if (rng == 11)  
{  
rng = Math.round(Math.random() \* 11);  
}

This will give you a randomizer that generates any number from 1-10 at an actual 10% each.

If someone knows of a way to do this without the workaround, please let me know. Otherwise, you can just do this. Hope you learned something, and thanks for reading.

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [March 19, 2016, 4:41am UTC](https://forum.kirupa.com/t/a-tip-when-using-math-random-in-as3/634359/2 "2016-03-19T04:41:48Z")

</div>

Just use `Math.random() * (max + 1) | 0` aka `Math.floor(Math.random() * (max + 1))`

Or for less predictable randomness:

```auto
import flash.crypto.generateRandomBytes

const
	min:uint = 0,
	max:uint = 10

for(var i:int = 0; i < 4000; i++){
	var bytes:ByteArray = generateRandomBytes(4)
	trace(((bytes.readUnsignedInt() / uint.MAX_VALUE) * (max - min + 1) | 0) + min)
}

```

---

<div class="post-metadata">

**Author:** ![Twisted4000](https://avatars.discourse-cdn.com/v4/letter/t/f08c70/32.png) [@Twisted4000](https://forum.kirupa.com/u/Twisted4000)\
**Post date:** [March 19, 2016, 5:05am UTC](https://forum.kirupa.com/t/a-tip-when-using-math-random-in-as3/634359/3 "2016-03-19T05:05:47Z")

</div>

I don’t quite understand what the (max + 1) means, and doesn’t Math.floor make it so it rounds down? Please explain, thank you.

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [March 19, 2016, 5:08am UTC](https://forum.kirupa.com/t/a-tip-when-using-math-random-in-as3/634359/4 "2016-03-19T05:08:27Z")

</div>

Math.random will give you 0.0 through 0.9999999999. Multiplying it by max, if you set max to 10, will give you 0.0 through 9.99999. Multiplying by max + 1 will get you 0.0 through 10.9999. Flooring the result will get you 0 through 10 as possible results.

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [March 19, 2016, 5:15am UTC](https://forum.kirupa.com/t/a-tip-when-using-math-random-in-as3/634359/5 "2016-03-19T05:15:41Z")

</div>

See also:

[https://www.kirupa.com/developer/actionscript/tricks/random.htm](https://www.kirupa.com/developer/actionscript/tricks/random.htm)  
[https://www.kirupa.com/flash/advanced\_random\_numbers\_as3.htm](https://www.kirupa.com/flash/advanced_random_numbers_as3.htm)

> **[True Random Integers in Flash AS3 - Scott Ruttencutter - Madison, WI iPhone...](http://www.ruttencutter.com/true-random-integers-in-flash-as3/)**
>
> So many people have this wrong, I have to post something right about this. I’m going to show you the example below and explain to you why it’s WRONG, and the simple change in logic required to make true random number generation happen for you. THIS...

  

> **[Math.random()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random)**
>
> The Math.random() function returns a floating-point, pseudo-random number in the range from 0 inclusive up to but not including 1 — which you can then scale to your desired range. The implementation selects the initial seed to the random number...

---

<div class="post-metadata">

**Author:** ![Twisted4000](https://avatars.discourse-cdn.com/v4/letter/t/f08c70/32.png) [@Twisted4000](https://forum.kirupa.com/u/Twisted4000)\
**Post date:** [March 19, 2016, 6:17am UTC](https://forum.kirupa.com/t/a-tip-when-using-math-random-in-as3/634359/6 "2016-03-19T06:17:17Z")

</div>

Ah, this works very well, thank you, that’s exactly what I was looking to do.

---

<div class="post-metadata">

**Author:** ![linusj](https://avatars.discourse-cdn.com/v4/letter/l/b2d939/32.png) [@linusj](https://forum.kirupa.com/u/linusj)\
**Post date:** [March 19, 2016, 8:02pm UTC](https://forum.kirupa.com/t/a-tip-when-using-math-random-in-as3/634359/7 "2016-03-19T20:02:49Z")

</div>

This was really neat.(from mozilla in @krilnon’s link)

```
// Returns a random integer between min (included) and max (included)
// Using Math.round() will give you a non-uniform distribution!
function getRandomIntInclusive(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}
```

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [March 19, 2016, 10:28pm UTC](https://forum.kirupa.com/t/a-tip-when-using-math-random-in-as3/634359/8 "2016-03-19T22:28:12Z")

</div>

It’s also basically what I posted 3 posts above the link you’re talking about. :trout: But yeah; it’s neat!

---

<div class="post-metadata">

**Author:** ![linusj](https://avatars.discourse-cdn.com/v4/letter/l/b2d939/32.png) [@linusj](https://forum.kirupa.com/u/linusj)\
**Post date:** [March 20, 2016, 8:29am UTC](https://forum.kirupa.com/t/a-tip-when-using-math-random-in-as3/634359/9 "2016-03-20T08:29:43Z")

</div>

I know, I know. But there was an extra +min that had me 😊

I never seen this ` | 0`. What does that mean?

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [March 20, 2016, 7:10pm UTC](https://forum.kirupa.com/t/a-tip-when-using-math-random-in-as3/634359/10 "2016-03-20T19:10:07Z")

</div>

`|` converts numbers into integers (and `floor`s them in the process). It’s also the bitwise `or` operator, but since it’s always paired with 0 in our context, it’s basically just serving as a replacement of `Math.floor`.

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [March 20, 2016, 8:42pm UTC](https://forum.kirupa.com/t/a-tip-when-using-math-random-in-as3/634359/11 "2016-03-20T20:42:16Z")

</div>

Show off…

---

<div class="post-metadata">

**Author:** ![linusj](https://avatars.discourse-cdn.com/v4/letter/l/b2d939/32.png) [@linusj](https://forum.kirupa.com/u/linusj)\
**Post date:** [March 21, 2016, 8:46am UTC](https://forum.kirupa.com/t/a-tip-when-using-math-random-in-as3/634359/12 "2016-03-21T08:46:57Z")

</div>

Hahaha!

I have to apologize, I really didn’t see your +min Krilnon. 🙂  
[http://forum.kirupa.com/uploads/kirupa/original/2X/0/0cb735b41b5fded4a11dec4156fea924e8e7360c.mov](https://forum.kirupa.com/uploads/kirupa/original/2X/0/0cb735b41b5fded4a11dec4156fea924e8e7360c.mov)
