elixir-lang/elixir — pull request #2584 by herenowcoder (2014-07-26 19:43:28+00:00)
Title: Enum.sample using reservoir sampling

Here's the PR for `Enum.sample` with single pass of the input collection, using [reservoir sampling](http://en.wikipedia.org/wiki/Reservoir_sampling), based on the experiments [we've talked about](https://github.com/elixir-lang/elixir/pull/2565).

Mentioning @joshnuss as he first came up with the topic, so the chance he needs the functionality :)

The algo has a funny corner case, that when the requested sample size is bigger than a collection size, the original collection is returned untouched - ie. not randomised. Is this desired behaviour for the API enduser?

If not, we can fallback to shuffling the sample buffer in such case, with a disadvantage that it will take another piece of time - but with the advantage that the input collection would not be walked again, because shuffling would be on the sample we already got.

A side note, how on earth is the random generator for doc_test seeded? :) I succeeded with in-doc examples only by trial and error ;)