Last active
August 17, 2021 19:52
-
-
Save eliardocosta/e0eb41dc588ee6c932cd043d37bed8b2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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