From The art of solving problems with Monte Carlo simulations
Generate uniform deviates until their sum exceeds one. The average number of deviates needed is Euler’s number, e. Proof.
program main
! simulate Euler's number, e
implicit none
integer, parameter :: nsim = 10000000, niter = 10
integer :: i,iter,j,nsum(nsim)
real :: xsum,xran
call random_seed()
do iter=1,niter
do i=1,nsim
xsum = 0.0
j = 0
do
j = j + 1
call random_number(xran)
xsum = xsum + xran
if (xsum > 1.0) exit
end do
nsum(i) = j
end do
print*,sum(nsum)/real(nsim)
end do
print*,exp(1.0)," true"
end program main
output:
2.71822739
2.71828914
2.71854210
2.71806741
2.71793246
2.71829391
2.71816230
2.71832871
2.71852446
2.71853471
2.71828175 true