# Digits of Pi

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?

# Method

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 pltimport numpy as npwith open('pi.txt') as f:    read_data = f.read()#       0  1  2  3  4  5  6  7  8  9data = [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)] += 1y_pos = np.arange(len(objects))plt.bar(y_pos, data)plt.xticks(y_pos, objects)plt.xlabel('Digit')plt.ylabel('Instances')plt.title('Digits of the first 1M of $\pi$')plt.show()

# Results

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.

# Conclusion

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.

Patrick is a software engineer from New England. Today, he resides in Seattle where he enjoys playing chess and ice hockey.

## More from Patrick Brown

Patrick is a software engineer from New England. Today, he resides in Seattle where he enjoys playing chess and ice hockey.

## Plotting a Quantitative Variable in Your Dataset

Get the Medium app