## Objective

To determine how likely it is for each digit of pi to appear by charting the digits of pi into a frequency graph.

• Are there patterns?
• If not, is it suitable for a random number generator?

## Method

The more data I can collect, the more apparent patterns -- if any -- will appear. In order to best do this I wrote a 32 line script in Python that can take in any number, at it will generate that many digits of pi. After that, it searches for each instance of each digit. For this project I pushed my computer to generate 1,000,000 digits of pi (I tried to do more, but it didn't turn out well).

``````from sys import argv

import numpy as np
from sympy.mpmath import mp
from matplotlib import pyplot as plt

digits = int(argv[1])

mp.dps = digits  # number of digits to search for
pi = list("{0}".format(mp.pi)) # store pi as an array of strings

#       0 1 2 3 4 5 6 7 8 9
data = [0,0,0,0,0,0,0,0,0,0]

for i in range(0,10):
data[i] = pi.count(str(i))
print i, data[i]

# plot bar graph
fig = plt.figure()
ax = plt.subplot(111)
ax.bar(range(len(data)), data)

# labels
plt.title('Instances of each digit in %d digits of Pi' % digits)
plt.ylabel('# of instances')
plt.xlabel('digit')
plt.xticks(np.arange(0, 10, 1))
data.sort()
plt.axis([0, 10, 0, data[-1] + digits/100])

plt.show()
``````

## Results

The results weren't very surprising. The biggest outliers in the bunch were the 6's by -452. Not too much considering we generated a million digits. What was interesting was that there was equal instances of 3's and 4's. The chances of that happening seem to be low, but it probably doesn't mean anything.

Digit # Of Instances Deviation
0 99959 -41
1 99757 -243
2 100026 +26
3 100230 +230
4 100230 +230
5 100359 +359
6 99548 -452
7 99800 -200
8 99985 -15
9 100106 +106

The graph is even more damning: Everything hovers around a hundred thousand, right where it's supposed to.

## Conclusion

The digits of pi do seem pretty evenly distributed, however, I don't think it's suitable for random number generation. While it's very close to even distribution, if you only use the first million digits of pi you have a higher chance of getting a 5 over the other numbers. As you go further out into pi, the numbers will get more evenly distributed, but there must be a much more efficient and accurate way to generate random numbers.

Edit: I got some complaints about the histogram having a deceptive scale: Sorry guys! It's fixed now!