> "Here we have a program that flips a fair coin n times":

with(stats):

CoinToss:=proc(n)
local T,i,L;
T:=[];
L:=[random[empirical[.5,.5]](n)];
for i from 1 to n
do
T:=[op(T),2*(L[i]-3/2)];
od;
T;
end:

"Here we have a program that gives the running total of a list of numbers":

RunTotal:=proc(L)
local n,T,V,i;
n:=nops(L);
T:=[];
V:=0;
for i from 1 to n
do
V:=V+L[i];
T:=[op(T),V];
od;
T;
end:

"Here we have a program that perform the follwing experiment m times: take a coin and flip it n times and records the total.":


ManyToss:=proc(n,m)
local i,T;
T:=[];
for i from 1 to m
do
T:=[op(T),RunTotal(CoinToss(n))[n]];
od;
T;
end:

> C1:=CoinToss(100);

C1 := [1, 1, 1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -...
C1 := [1, 1, 1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -...
C1 := [1, 1, 1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -...

> RunTotal(C1);

[1, 2, 3, 2, 3, 2, 3, 2, 1, 0, -1, -2, -3, -4, -5, ...
[1, 2, 3, 2, 3, 2, 3, 2, 1, 0, -1, -2, -3, -4, -5, ...
[1, 2, 3, 2, 3, 2, 3, 2, 1, 0, -1, -2, -3, -4, -5, ...
[1, 2, 3, 2, 3, 2, 3, 2, 1, 0, -1, -2, -3, -4, -5, ...

> MT1:=ManyToss(50,100);

MT1 := [4, 2, -4, 8, 2, 4, 6, 10, -10, 0, -2, 4, 2,...
MT1 := [4, 2, -4, 8, 2, 4, 6, 10, -10, 0, -2, 4, 2,...
MT1 := [4, 2, -4, 8, 2, 4, 6, 10, -10, 0, -2, 4, 2,...

> "Notice the colon supresses having to view the list, but the data is still produced after you hit return and the resulting list is called MT1":

MT1:=ManyToss(50,1000):

> "If we'd like to see the 671st experments value we could hit return after typing in.":

MT1[671];

-4

>