Test for randomness in the digits of π.

Run the code to find the numerical value of π to 50 digits. Try getting more than 50 digits—for example, 1,000:

Pi represents the exact value of π . It can’t be represented by a finite number of digits; that’s why you don’t see numerical digits when you evaluate it:

To turn Pi into a number, you have to specify how precise an approximation you want. This gives a 50-digit approximation to Pi:

Get a list of the first 100 digits of π in base 10. Try bases other than 10—for example, 2:

This gives a list of the first 4 digits of π in base 10, followed by the number of digits to the left of the decimal point:

Use First if you want just the list of digits:

Count how often 0 through 9 occur in the first 100 digits. Try more digits than 100:

Counts counts how often each element of a list occurs. This list has four ones and three twos:

Make a bar chart of 0 through 9 frequencies. See if it evens out with more than 100 digits:

Counts returns key-value pairs. In this case, the keys are the digits and the values are the number of times they occur:

KeySort sorts by the keys, so you get the digit counts in order:

This makes a chart of digit counts in digit order from 0 to 9. Mouse over a bar to see the numerical count:

Make a 20×20 array of π digits. Try sizes other than 20—for example, 40:

Get the first 9 digits of π:

Partition the list into sublists of length 3:

Format the list of lists as a grid. Each sublist is a row in the grid:

To make it easier to change the size of the grid, factor out the size using With. For an n×n grid of digits, you need n*n digits partitioned into lists of length n:

Format that as a grid:

Now you can easily change the size of the grid from 3×3 to 4×4:

Visualize the array of digits. Try a larger array than 20×20:

Use ArrayPlot in place of Grid to show the digits graphically. Lower numbers are lighter and higher numbers are darker. Can you read off the digits {3,1,4,1,5,9,…} in the first row?

The irregular, speckled appearance of the grid suggests that the digits of π are random. For comparison, here is a plot of the digits of 1/7, which are not random:

Add color:

Specifying ColorFunctionHue makes a plot with colors instead of gray values. It shows the randomness of the digits of π, but it’s not as easy to make the correspondence between colors and digits as it is between gray values and digits:

Make a “π random walk”. Try it for more than 100 digits:

This gives the first 100 digits of π in base 2:

Subtracting .5 from each digit gives -.5 for a 0 and .5 for a 1:

Accumulate gives successive sums of the items in the list, namely {.5,.5+.5,.5+.5-.5,…}:

If the digits of π are truly random, you’d expect there to be about as many 0s as 1s, which means that the accumulated sums should hover around zero.

Plot the sums to see if that’s the case:

They seem to be drifting away from 0. Does that trend continue? Have a look at more digits (1,000 this time):

You can do the same analysis in other bases. Just remember to subtract the average of the digits up to the base 1. For base 3 digits, you need to subtract (0+1+2)/3=1 (if the base is b, the average of the base b digits is (b-1)/2):