Skip to content

Instantly share code, notes, and snippets.

@JEG2
Last active March 4, 2023 18:26
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 JEG2/13fc565c848b369de8afd352e3e52d09 to your computer and use it in GitHub Desktop.
Save JEG2/13fc565c848b369de8afd352e3e52d09 to your computer and use it in GitHub Desktop.
Experimenting with cumulative probability
trials = 10_000
simulation =
4
|> Stream.iterate(fn dollars ->
case :rand.uniform(4) do
1 -> dollars - 1
2 -> dollars + 1
_n -> dollars
end
end)
|> Stream.with_index()
|> Stream.map(fn
{-1, i} -> i
state -> state
end)
Stream.repeatedly(fn -> Enum.find(simulation, &is_integer/1) end)
|> Stream.take(trials)
|> Enum.sum()
|> then(&(&1 / trials))
|> IO.inspect()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment