Site icon R-bloggers

Men who stare at needles

[This article was first published on progRamming for the fun of it , and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Buffon’s needle problem is a question first posed in the 18th century by Georges-Louis Leclerc, Comte de Buffon:

What is the probability that a needle thrown at a lined sheet of paper will cross a line?

This problem can be used to estimate π. If we set the nail size and the line distance = 1, the estimator can be calculated by:
$$\widehat{\pi}=\frac{2\cdot throws}{hits}$$
Because throwing 80.000 needles can be annoying, I created an implementation in R. The result took over two hours to render an is attached to this post. It was generated by saving multiple GIF files and combining them to a movie. Red needles are hits, the heading represents the current approximation of π.




After loading the function you can start a simulation by typing:

 buffon([number of simulations], [number of needles], [c(seeds)])

The following command will create one simulation with 100 needles: And here’s the function:

For more information on R and what to do with it visit: www.r-bloggers.com
To leave a comment for the author, please follow the link and comment on their blog: progRamming for the fun of it .

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.