Skip to content

Instantly share code, notes, and snippets.

@eliardocosta
Last active August 17, 2021 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eliardocosta/e0eb41dc588ee6c932cd043d37bed8b2 to your computer and use it in GitHub Desktop.
Save eliardocosta/e0eb41dc588ee6c932cd043d37bed8b2 to your computer and use it in GitHub Desktop.
rNx <- function(k = 1E3, x = 1) {
out <- numeric()
for (i in 1:k) {
nx <- 1
u <- runif(1)
while(u <= x) {
u <- u + runif(1)
nx <- nx + 1
}
out <- append(out, nx)
}
return(out)
}
N_1 <- rNx() # random sample of size 1000 from the distribution of N_1
mean(N_1) # estimate of 'e' by Monte Carlo
exp(1) # value of 'e'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment