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