#' Buffon's needle
#'
#' This function simulates the mathematics problem, Buffon's needle,
#' proposed by Georges-Louis Leclerc, Comte de Buffon.
#'
#' @param drops The number of needle drops to simulate.
#' @param w The width of the boards or distance between lines.
#' @param n The length of the needle.
#' @export
#' @return A rational approximation of π, calculated as drops / hits.
#' @examples buffon(10000)
buffon <- function(drops, w = 2, n = 1) {
hits <- 0
for (i in 1:drops) {
d <- runif(1, min = 0, max = w / 2)
theta <- runif(1, min = 0, max = pi)
if (d <= 0.5 * n * sin(theta)) {
hits <- hits + 1
}
}
drops / hits
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.