Hi,
when I run it it does print out the result and gives the output Null.
If you only want the output you might want to use:
needleCross[length_, distance_] :=
{Block[{midPoint = RandomReal[distance], rotationAngle = RandomReal[{-Pi/2, Pi/2}]},
xCoordLinePoints = {midPoint - (Cos[rotationAngle]*length/2), midPoint + (Cos[rotationAngle]*length/2)}];
If[xCoordLinePoints[[1]] <= 0 || xCoordLinePoints[[2]] >= distance, "Line Crossed", "No Line Crossed"]}[[1]];
So now you can simulate Buffon's needle experiment to estimate Pi. We know that
$\pi \approx \frac{2 \cdot length}{distance \cdot P}$. P is the probability to hit a line. So lets get an estimate for P.
If your function is correct then
M = 1000000; P = N[(Count[Table[needleCross[1, 1.2], {M}], "Line Crossed"]/M), 10]
should give you an estimate of the probability that a needle crosses a line. Then an estimate of Pi should be
2./(1.2*P)
which in my case gives 3.13967, 3.1468, 3.13999, 3.13775, 3.14344 for a couple of different runs.
If we assume someone really keen on the experiment would run it 400 times on 8000 needles. Then we would get:
estimatesP = Monitor[Table[M = 8000; P = N[(Count[Table[needleCross[1, 1], {M}], "Line Crossed"]/M), 10], {i, 1, 400}], i];
piestimates = 2./(1*#) & /@ estimatesP;
dist = SmoothKernelDistribution[piestimates]; Plot[
PDF[dist, x], {x, 3.03, 3.28},
Epilog -> Line[{{Mean[dist], 0}, {Mean[dist], 20}}],
Filling -> Bottom, PlotTheme -> "Marketing",
LabelStyle -> Directive[Bold, Medium], ImageSize -> Large]
The expected value is:
Mean[SmoothKernelDistribution[piestimates]]
or equivalently
Mean[piestimates]
which equals 3.14029.
If we assume that the values are Gaussian distributed we can also represent that:
Plot[PDF[EstimatedDistribution[piestimates,
NormalDistribution[\[Mu], \[Sigma]]], x], {x, 3.03, 3.28},
Epilog -> Line[{{Mean[dist], 0}, {Mean[dist], 20}}],
Filling -> Bottom, PlotTheme -> "Marketing",
LabelStyle -> Directive[Bold, Medium], ImageSize -> Large]
Cheers,
Marco