// The goal was to calculate Benford's distribution which is distribution of first digits. // Michael Cammer 20130102 // rewritten 20151230 // see http://mathworld.wolfram.com/BenfordsLaw.html var distribution = newArray(10); // this is the frequency of each digit from 0 to 9 /* A "random" number which is a decimal is generated. It is multipled by 1000000 and the decimal portion lopped off. This is our integer to be tested. */ macro "calculate Benfords distribution" { Array.fill(distribution, 0); // clear the array for(i=0; i<10000;i++) { x = round( 1000000*random()*random()*random()*random()*random()*random()*random()*random()*random() ) ; if (x>10) calculateFirstDigit(x); } // for i for (i=0; i<10; i++) print(i+" \t"+distribution[i]); selectWindow("Log"); } //======================================================== function calculateFirstDigit(n) { if (n > 9) calculateFirstDigit(floor (n/10)); else distribution[n] ++; } // calculateFirstDigit(n)