How random is random?

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:

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.

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…

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?

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 -

Math.random()*-1

*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?

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

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

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

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.
:slight_smile:

*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.
:slight_smile: **

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.

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

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

*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

*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

interesting! =)