Monday, September 7, 2009

I'm not a math geek but...

Filed under Code monkey

I orginally found this post at at: http://blog.mogrify.org/2007/03/08/im-not-a-math-geek-but/

but for some reason it's now showing "Page Not Found". If anyone knows where the orginal article now resides please let me know and I'll link to it properly.

It was an interesting study of random numbers in javascript:

I'm not a math geek, but occasionally something peeks my interest. Today I was trying to work out a good way to do something randomly about once every nine or ten times. I decided to check the last digit of the return value of Javascript's Math.random() function, which returns a number between 0 and 1. Like this: 0.1809368982206232 I knew it would never be zero, since the trailing zero would be removed; so I figured I could pick any other digit and I'd be right about once every nine times. Turns out it doesn't work like that. The frequency distribution of the last digit is uneven.

Example Code:

for (var num=1; num<=9; num++) {
var match = 0; var re = new RegExp(num+'$');
for (var i=0; i<10000; i++) {
if (re.test(Math.random())) {
match++;
}
} document.write("" + num + ": " + (match/i*100+'').substring(0,5) + "%");
}

This script calls Math.random() 10,000 times for each digit from 1 to 9. It prints the last digit match rate as a percentage. Its output looks like this:

1: 8.790% 2: 10.4% 3: 12.19% 4: 13.17% 5: 13% 6: 12.95% 7: 11.51% 8: 9.35% 9: 8.58%

You'd expect something around 11.11% (one of nine) for each digit; but you can see that the frequency increases until 4, and then decreases again. In fact, this looks a heck of a lot like a bell curve, ergo, a Gaussian distribution.

I found that if I check the first digit after the decimal point, I get what I'm expecting: a list of roughly equal values somewhere around 11.11% for each digit. So although the output of Math.random() is uniformly distributed, the last digits are normally distributed.

I can't grok this much further than that; perhaps it's got something to do with the way the numbers are generated, or maybe that's just The Way Things Are. If anyone can explain to me why this happens, leave a note. Maybe I'll go poke around in the Gecko source code or something.

1 comment:

Anonymous said...

I've read your article a few days ago and I was wondering if you have found the answer.
I haven't got numbers but I have think about it and this is what I got to using the last digit is always wrong because you don't
know what's after that .

Ex: you deal with this number 0.465 and let's say you just need 2 digit you can chose between 0.46 and 0.47, in physics experiments the rule is to use the
closer even number (don't ask me way) hence 0.46 but there are also other rules that can be applied that could lead to 0.47 . -
In any case you apply a rule and that's way you don't have a stochastic process any more. computer randomness doesn't exist and for that reason is always better using the entire random
number as a seed rather that using a digit of it.(same thing :you chose a specific digit, you determinate it and this means that you are doing the opposite
of how a stochastic process should be) - in my opinion using the fist decimal digit is a mistake even if results are good in this particular case.