clc,clear
%Note that the condition 'an A4 paper' is in fact not used.
n=1e5;
l=7;
a=5;
s=0;
for i=1:1:n
x=l+l*rand; %If we use 'x=3*l*rand', we should study the cases where x=0 and x=3*l.
%y=29.7*rand; The value of y is in fact useless.
theta=pi*rand;
d1=abs(x-l);
d2=abs(x-2*l);
theta1=pi-acos(d1/a);
theta2=acos(d2/a);
if theta
s=s+0;
else
s=s+1;
end
end
p=2*a*n/l/s;
er=abs(p-pi);
fprintf('Pi=%f with n=%.e and er=%.e\
\
',p,n,er);
Just for fun :D