# How random is random?

**URL:** <https://forum.kirupa.com/t/how-random-is-random/22225>\
**Category:** flash\
**Created:** [June 1, 2003, 9:20am UTC](https://forum.kirupa.com/t/how-random-is-random/22225 "2003-06-01T09:20:01Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![jingman](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/jingman/32/163_2.png) [@jingman](https://forum.kirupa.com/u/jingman)\
**Post date:** [June 1, 2003, 9:20am UTC](https://forum.kirupa.com/t/how-random-is-random/22225/1 "2003-06-01T09:20:01Z")

</div>

I’ve always been interested in how computers/flash generate random numbers, so I ran a little test.

Code is this:

[AS]onEnterFrame = function(){  
count += 1;  
sum = oldsum+Math.random()\*10;  
oldsum = sum;  
avg = sum/count;  
trace(avg);  
}[/AS]

I let debugger run for a long darn time, then cut and paste the output to excel. Results below:

![](http://www.pearsandrulers.com/temp/true_random.jpg)

Hmm, true average should be 5 right? This average is about 4.975, and it only stabalizes after 7500 or so steps. Before that the average really seems to roam - and not even randomly! The average seems to climb and fall in 1500 cycle sections.

I don’t know what this means, just thought it was interesting.

---

<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:** [June 1, 2003, 12:22pm UTC](https://forum.kirupa.com/t/how-random-is-random/22225/2 "2003-06-01T12:22:21Z")

</div>

well, also consider that Math.random() returns from 0.0 to 9.9 and in extremely rare cases, 1.0

that’s a pretty nice find though. try using random(10)+1 instead and see what happens…

---

<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:** [June 1, 2003, 5:20pm UTC](https://forum.kirupa.com/t/how-random-is-random/22225/3 "2003-06-01T17:20:55Z")

</div>

Interesting observation!

I thought it generated random numbers beween 0 and 1 - I have never come across either of the two extremes.

Just a thought - how could one possible generate a random negative number?

---

<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:** [June 1, 2003, 6:47pm UTC](https://forum.kirupa.com/t/how-random-is-random/22225/4 "2003-06-01T18:47:40Z")

</div>

Math.random() does generate a random number between 0 and 1, I just multiplied it by 10… for fun I guess.

To generate a random negative number -

```auto
Math.random()*-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:** [June 1, 2003, 6:59pm UTC](https://forum.kirupa.com/t/how-random-is-random/22225/5 "2003-06-01T18:59:34Z")

</div>

> \*Originally posted by thor \*  
> \*\*well, also consider that Math.random() returns from 0.0 to 9.9 and in extremely rare cases, 1.0

that’s a pretty nice find though. try using random(10)+1 instead and see what happens… \*\*

Yes but… isn’t the chance of getting a “1” the same as the chance of getting any other number (if it were truely random)?

So depending on how many decimals it carries out for the Math.random() function, you should have a 1/x chance of getting each number between 0 and 9.9999999999… shouldn’t you?

---

<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:** [June 1, 2003, 7:34pm UTC](https://forum.kirupa.com/t/how-random-is-random/22225/6 "2003-06-01T19:34:39Z")

</div>

using random(11) - Generating integers between and including 0 through 10 - average should be 5. 20,000 iterations.

![](http://www.pearsandrulers.com/temp/true_random_11.jpg)

Oh, and my code looks like this now (faster):

```auto
for(i=0; i<9999; i++){
	trace(random(11));
}

```

---

<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:** [June 1, 2003, 9:12pm UTC](https://forum.kirupa.com/t/how-random-is-random/22225/7 "2003-06-01T21:12:39Z")

</div>

that’s 10.000 iterations…  
why should random be an average of 5, this math probability thing contradicts the functions name, if it’s random, then it’s random, could be 0.9 all the time, just like the lottery…you never win.  
🙂

---

<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:** [June 1, 2003, 9:25pm UTC](https://forum.kirupa.com/t/how-random-is-random/22225/8 "2003-06-01T21:25:21Z")

</div>

> \*Originally posted by eyezberg \*  
> \*\*that’s 10.000 iterations…  
> why should random be an average of 5, this math probability thing contradicts the functions name, if it’s random, then it’s random, could be 0.9 all the time, just like the lottery…you never win.  
> 🙂 \*\*

The code is for 10,000 iterations, the graph shows 20,000 iterations (I ran it twice, since the dubug window maxes out at 9,999).

Random means it has a probability of 1/x where x is the number of possible values. random(11) is like rolling an 11 sided die.

The average should be 5 because if there is an equal probability of getting any integer between and including 0 and 10, the average of an infinite number of iterations should be (10+9+8+7+6+5+4+3+2+1+0)/11 = 5.

---

<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:** [June 1, 2003, 10:52pm UTC](https://forum.kirupa.com/t/how-random-is-random/22225/9 "2003-06-01T22:52:17Z")

</div>

eyez \>\> The graph corresponds to the first code, not the simple trace.

Jingman \>\> I think that Math.random() can never return 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:** [June 2, 2003, 12:28am UTC](https://forum.kirupa.com/t/how-random-is-random/22225/10 "2003-06-02T00:28:35Z")

</div>

> \*Originally posted by ilyaslamasse \*  
> \*\*  
> Jingman \>\> I think that Math.random() can never return 1. \*\*  
> yes, that’s true. reason? macromedia noticed that Math.random() was spitting out 1.0, which it was not supposed to, in flash player 6 r30, which they considered a major bug so they fixed it in the next release. i read this fact in several places cause i too thought that that function would return 1.0.  
> also, straight from the help files:  
> Method; returns n, where 0 \<= n \< 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:** [June 2, 2003, 12:30am UTC](https://forum.kirupa.com/t/how-random-is-random/22225/11 "2003-06-02T00:30:51Z")

</div>

> \*Originally posted by ilyaslamasse \*  
> \*\*eyez \>\> The graph corresponds to the first code, not the simple trace.

Jingman \>\> I think that Math.random() can never return 1. \*\*

I think you’re right:

From reference: Method; returns n, where 0 \<= n \< 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:** [June 2, 2003, 1:12am UTC](https://forum.kirupa.com/t/how-random-is-random/22225/12 "2003-06-02T01:12:57Z")

</div>

interesting! =)
