View Full Version : How random is random?
jingman
June 1st, 2003, 05:20 AM
I've always been interested in how computers/flash generate random numbers, so I ran a little test.
Code is this:
onEnterFrame = function(){
count += 1;
sum = oldsum+Math.random()*10;
oldsum = sum;
avg = sum/count;
trace(avg);
}
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.
thoriphes
June 1st, 2003, 08:22 AM
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...
catreya
June 1st, 2003, 01:20 PM
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?
jingman
June 1st, 2003, 02:47 PM
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
jingman
June 1st, 2003, 02:59 PM
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?
jingman
June 1st, 2003, 03:34 PM
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):
for(i=0; i<9999; i++){
trace(random(11));
}
eyezberg
June 1st, 2003, 05:12 PM
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.
:)
jingman
June 1st, 2003, 05:25 PM
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.
pom
June 1st, 2003, 06:52 PM
eyez >> The graph corresponds to the first code, not the simple trace.
Jingman >> I think that Math.random() can never return 1.
thoriphes
June 1st, 2003, 08:28 PM
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
jingman
June 1st, 2003, 08:30 PM
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
senocular
June 1st, 2003, 09:12 PM
interesting! =)
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.