How likely it is for each digit of pi to appear? Let’s find out by charting the digits of pi into a frequency graph.
- Are there patterns?
- If not, is it suitable for random number generation?
The more data I can collect, the more apparent patterns (if any) will appear. I wrote this Python script (below) that searches for each instance of each digit. For this project I will analyze 1,000,000 digits of pi.
import matplotlib.pyplot as plt
import numpy as np
with open('pi.txt') as f:
read_data = f.read()
# 0 1 2 3 4 5 6 7 8 9
data = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
objects = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
for c in read_data:
if c in objects: data[int(c)] += 1
y_pos = np.arange(len(objects))
plt.title('Digits of the first 1M of $\pi$')
The results indicated that there was a very even distribution of digits. In a perfect distribution we’d see exactly 100,000 in each column, which we got pretty close to. The biggest outlier in the bunch were the 6’s by -452. Not too much considering we generated a million digits.
The digits of pi do seem pretty evenly distributed, however, I don’t think we’ve concluded whether it’s suitable for random number generation. While it’s very close to even distribution, only perfect distribution will suffice. For example, if you only use the first million digits of pi you have a higher chance of getting a 5 over other numbers. Perhaps given an arbitrary length of pi we could approach perfect distribution, but that’s an article for another day.
Update 10/14/2017: This person is way more credentialed to speak to the randomness of pi. If my article intrigued you, definitely check theirs out.