Thanks for the comments and the corrections for the boundary effects! No debate about this.

]]>I know this is like 3 years late, but I thought I should post anyway. I’m putting together a poster on Monte Carlo methods for high school students to accompany an activity on Buffon’s needle, and I really liked your 4 methods of approach, and I’ll be adapting them for a section of the poster.

However, I do think you’re making some systematic errors. For example, in the first one, by restricting the centre to be between l and d-l, you’re completely discounting needles that are within the board (that can be 0.01 away from the bottom but have an angle such that they don’t cross, say 0 degrees). As a result, you underestimate the percentage of crosses

Rewriting your code using floor instead of trunc (for negatively placed needles) in numbhits (can be interpreted as rethrowing them if center doesn’t lie in the range etc) and a proper uniform distribution 0,d, for 10^6 trials, you do get quite close to pi. Similarly for part 2.

U=runif(N,min=0,max=D) #centre

Similarly for 3 and 4, I was a bit confused where the range equation came from for the centres. For each angle (on a square board), it’s range is given by max(d/sin(O), d/cos(O)) obtaining instead:

U=runif(N)*(D*apply(cbind(1/sin(O),1/cos(O)),1,max)) #centre

#Alternatively mirror in pi/4 and stick with one.

Again, running this code now, (without the obvious circle range) now gives close to pi when run 10^6 times ~ 3.144261

Overall, I don’t want to appear to be too critical (and I could be wrong), but I’m quite sure with my results.

Either way, thanks, the visualisations were superb and the code was easy to read and modify, and I’m certainly going to discuss the angle origin variant (3 and 4) and maybe introduce some white noise on it’s final position!

]]>