So I recently came across Benford's law and immediately tried to code it out but the answer I got was rather confusing. I think my code is correct I'm pretty sure it is but the result is just not the one others get. Here's the code:
int a;
float[] flag=new float[10];
for(int i=0;i<10000;i++)
{
a=floor(random(0,99999));
while(a>=10)
a/=10;
flag[a]++;
}
But I get roughly the same flag[k] (around 1100) but according to Benford's law that should not be the case.
I think it is because my upper limit is set so I tried modifying my code as follows :
int a;
float p,q,r;
float[] flag=new float[10];
for(int i=0;i<10000;i++)
{
r=random(0,99999)
q=random(0,r);
p=random(0,q);
a=floor(random(0,p));
while(a>=10)
a/=10;
flag[a]++;
}
And this gives really satisfactory results, the problem is that this creates a very different type of distribution, it's certainly not random, so the problem I'm running into is that: is this how Benford's law work? Since we don't know the "Cutoff" it makes 1s significantly more likely than 9s and other numbers, Since they would require the "Cutoff" to be high which reduces the probability of them occuring in a random "Cutoff" scenario?
Benford's law does not apply to data with a uniform distribution, be it continuous or discrete. It applies to data whose logarithms' fractional parts have a continuous uniform distribution.