JuliaStats/Distributions.jl — pull request #167 by one-more-minute (2013-12-20 15:58:04+00:00)
Title: Implement ordered sampling

I've implemented an extra option for `sample`, `ordered`, which returns the samples in the same order they were given:

```julia
sample(1:10, 5, ordered=true)
[2,6,8,10,10]

sample(1:10, 5, ordered=true, replace=false)
[1,5,6,8,9]
```

Incidentally, the ordered sampling without replacement algorithm is faster than Fisher-Yates, especially for a large number of samples; it could potentially replace it as the default.

```julia
@time sample(1:10^7, 9*10^6, ordered=true, replace=false);
# elapsed time: 0.17311315 seconds (72000416 bytes allocated)

@time sample(1:10^7, 9*10^6, ordered=false, replace=false);
# elapsed time: 0.928791431 seconds (152000488 bytes allocated)
```