Skip to content

Instantly share code, notes, and snippets.

@hakank
Created April 27, 2011 18:14
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 hakank/944824 to your computer and use it in GitHub Desktop.
Save hakank/944824 to your computer and use it in GitHub Desktop.
Gambler's Ruin simulation in K (Kona)
/ generate 10 random -1, 1 and
/ and count cumulative
+\ 10 ? (-1;1)
/ sample result
/ -1 0 1 0 1 2 1 2 1 2
/ just the last result:
+/x: 100 ? (-1;1)
/ sample result:
/ 2
/ define two help functions
mean: {+/x % #x}
table:{t[<t:(s:?x),'#:'=x]}
/ run the simulation of 100 games 100 times
/ and show the frequency distribution of the
/ results
table @ {+/100 ? (-1;1)} ' !100
/ sample result
/ (-26 1
/ -22 2
/ -16 2
/ -14 5
/ -12 4
/ -10 3
/ -8 5
/ -6 8
/ -4 11
/ -2 10
/ 0 6
/ 2 9
/ 4 7
/ 6 6
/ 8 6
/ 10 4
/ 12 3
/ 14 3
/ 16 3
/ 18 1
/ 20 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment