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.