Skip to content

Instantly share code, notes, and snippets.

@epfahl
Created November 29, 2023 13:13
Show Gist options
  • Save epfahl/67a370832d634f2937cec74c411d300f to your computer and use it in GitHub Desktop.
Save epfahl/67a370832d634f2937cec74c411d300f to your computer and use it in GitHub Desktop.
Implementation of von Neumann's exponential sampling algorithm.
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