Created
November 29, 2023 13:13
-
-
Save epfahl/67a370832d634f2937cec74c411d300f to your computer and use it in GitHub Desktop.
Implementation of von Neumann's exponential sampling algorithm.
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
defmodule Exp do | |
def draw() do | |
x = :rand.uniform() | |
draw(x, x, 1, 0) | |
end | |
defp draw(x, u, n, k) do | |
u_next = :rand.uniform() | |
if u > u_next do | |
draw(x, u_next, n + 1, k) | |
else | |
if rem(n, 2) == 0 do | |
x = :rand.uniform() | |
draw(x, x, 1, k + 1) | |
else | |
x + k | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment