# Error in a simulation of Buffon's needle problem for lattice

11 views (last 30 days)
on 10 Aug 2022
Commented: Walter Roberson on 10 Aug 2022
Heello, every one. I had a proble in a script which is for a simulation of Buffon's needle problem for lattice.
I am a beginner, so I tried to see what I could do.
When I run the script, I cannot get a correct output.
I basically created the code based on the attached link.
N = 1000;
L = 0.20;
xb = L + rand(1,N)*(1-2*L);
yb = L + rand(1,N)*(1-2*L);
angs = rand(1,N)*360;
xe = xb + L*cosd(angs);
ye = yb + L*sind(angs);
ax = axes;
plot(ax,[xb;xe],[yb;ye])
axis square
hold on
glines = 0:L:1;
for i = 1:length(glines)
xline(ax, glines(i));
end
for p = 1:length(glines)
yline(ax,glines(p));
end q = sum(floor(xb/L) ~= floor(xe/L));
r = sum(floor(yb/L) ~= floor(ye/L));
n = plus(q,r);
k = (n-q&r)/N;
disp(k);
1.0000e-03
##### 2 CommentsShowHide 1 older comment
Walter Roberson on 10 Aug 2022
k = (n-q&r)/N;
That line is suspicious. It is calculating
((n~=q) and (r~=0))/N