rand
rand copied to clipboard
Find a good design for Sample
Sample
was added in ce7c277b399941e9dc949893fe405610004dbcc2, however there are several problematic things:
- as of go1.18, compiler can't allocate the result on the stack, even when
Sample
was inline and thek
is constant (!); see:- https://github.com/golang/go/issues/20533
- https://github.com/golang/go/issues/27625
- however, changing the signature introduces inconsistency with
Perm
- the map is not reused between different calls
- however, placing it into
Rand
increases its size, and introduces a pointer in a previously pointer-free struct
- however, placing it into
- Floyd's sampling algorithm does not guarantee random order, so people are required to read the docs and don't forget to
Shuffle
if required - potential
PartialShuffle
is much faster and guarantees random order (although it requires mutable slice of sizen
) - having all 4 of
Perm
,Sample
,PartialShuffle
andShuffle
is maybe too much
On the other hand, Sample
runs in O(k) for both time and memory. Also, algorithm is simple but non-obvious (a bit like Shuffle
), as is the linear-search-instead-of-map optimization.
Also, with #1, Sample
may be not required at all.