Try now in the
Open Cloud »
(no sign-in required)

Randomness of Pi

Test for randomness in the digits of π.

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

SHOW/HIDE DETAILS

Pi represents the exact value of π . It cant be represented by a finite number of digits; thats why you dont see numerical digits when you evaluate it:

In[1]:=
X
Out[1]=

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

In[2]:=
X
Out[2]=

HIDE DETAILS
In[1]:=
X
Out[1]=

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

SHOW/HIDE DETAILS

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:

In[1]:=
X
Out[1]=

Use First if you want just the list of digits:

In[2]:=
X
Out[2]=

HIDE DETAILS
In[1]:=
X
Out[1]=

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

SHOW/HIDE DETAILS

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

In[1]:=
X
Out[1]=

HIDE DETAILS
In[1]:=
X
Out[1]=

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

SHOW/HIDE DETAILS

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

In[1]:=
X
Out[1]=

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

In[2]:=
X
Out[2]=

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

In[3]:=
X
Out[3]=

HIDE DETAILS
In[1]:=
X
Out[1]=

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

SHOW/HIDE DETAILS

Get the first 9 digits of π:

In[1]:=
X
Out[1]=

Partition the list into sublists of length 3:

In[2]:=
X
Out[2]=

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

In[3]:=
X
Out[3]=

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:

In[4]:=
X
Out[4]=

Format that as a grid:

In[5]:=
X
Out[5]=

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

In[6]:=
X
Out[6]=

HIDE DETAILS
In[1]:=
X
Out[1]=

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

SHOW/HIDE DETAILS

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?

In[1]:=
X
Out[1]=

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:

In[2]:=
X
Out[2]=

HIDE DETAILS
In[1]:=
X
Out[1]=

Add color:

SHOW/HIDE DETAILS

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

In[1]:=
X
Out[1]=

HIDE DETAILS
In[1]:=
X
Out[1]=

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

SHOW/HIDE DETAILS

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

In[1]:=
X
Out[1]=

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

In[2]:=
X
Out[2]=

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

In[3]:=
X
Out[3]=

If the digits of π are truly random, youd 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 thats the case:

In[4]:=
X
Out[4]=

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

In[5]:=
X
Out[5]=

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):

In[6]:=
X
Out[6]=

HIDE DETAILS
In[1]:=
X
Out[1]=