I thought I could try this really easily in Python.
Here's what I did:
Open IDLE and type:
from random import seed
from random import random
seed(1)
You can then try typing random() and get values: >>>random()
0.13436424411240122
>>>random()
0.8474337369372327
>>>random()
0.763774618976614
I actually forgot that random() doesn't just return an integer, like in c where if you type rand() you get a random integer. But these numbers looked great to me. I could easily imagine the first one 0.13436424411240122 just being 13436424411240122 and so on. I thought it's a great way to get the first digit of a random numbers with an unlimited size, and for this result I thought this is a good way to go ahead.So with the above basis, I want to look at the distribution of the first nonzero digit after the decimal point.
So next put the following function in. (basically it just gets a random as above, but keeps multiplying by 10 as long as the first digit of the string conversion is a "0". this returns the first non-zero digit at the left.)
def getone():
floaty = random()
while(str(floaty)[0] == '0'):
floaty *= 10
return int(str(floaty)[0])
This now gets us the leftmost digit, check it out: >>>getone()
3
>>>getone()
2
>>>getone()
4
>>>getone()
2
All right. Now let's just do this a few million times and keep track of buckets. Make some buckets: buckets = [0,0,0,0,0,0,0,0,0,0] # buckets for: 0,1,2,3,4,5,6,7,8,9
This next line will run for up to a few minutes: for tries in range(10000000):
buckets[getone()] += 1
Now print the results: for result in range(10):
print (result, ":", buckets[result])
This gets me.... 0 : 0
1 : 1112927
2 : 1110821
3 : 1109741
4 : 1110793
5 : 1111604
6 : 1112178
7 : 1111272
8 : 1110402
9 : 1110262
Clearly Benford's law does NOT apply here.Why not?