Skip to content

Instantly share code, notes, and snippets.

@aphyr
Created May 2, 2015 00:14
Show Gist options
  • Save aphyr/4b52097592c82101c173 to your computer and use it in GitHub Desktop.
Save aphyr/4b52097592c82101c173 to your computer and use it in GitHub Desktop.
(defn mostly-small-nonempty-subset
"Returns a subset of the given collection, with a logarithmically decreasing
probability of selecting more elements. Always selects at least one element.
(->> #(mostly-small-nonempty-subset [1 2 3 4 5])
repeatedly
(map count)
(take 10000)
frequencies
sort)
; => ([1 3824] [2 2340] [3 1595] [4 1266] [5 975])"
[xs]
(-> xs
count
inc
Math/log
rand
Math/exp
long
(take (shuffle xs))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment