2013-04-10 21:29:02 -07:00
|
|
|
import std.stdio, std.random;
|
|
|
|
|
|
|
|
|
|
auto sofN_creator(in int n) {
|
|
|
|
|
size_t i;
|
|
|
|
|
int[] sample;
|
|
|
|
|
|
|
|
|
|
return (in int item) {
|
|
|
|
|
i++;
|
|
|
|
|
if (i <= n)
|
|
|
|
|
sample ~= item;
|
2015-02-20 00:35:01 -05:00
|
|
|
else if (uniform01 < (double(n) / i))
|
2013-04-10 21:29:02 -07:00
|
|
|
sample[uniform(0, n)] = item;
|
|
|
|
|
return sample;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
|
enum nRuns = 100_000;
|
|
|
|
|
size_t[10] bin;
|
|
|
|
|
|
2014-04-02 16:56:35 +00:00
|
|
|
foreach (immutable trial; 0 .. nRuns) {
|
|
|
|
|
immutable sofn = sofN_creator(3);
|
2013-04-10 21:29:02 -07:00
|
|
|
int[] sample;
|
2014-04-02 16:56:35 +00:00
|
|
|
foreach (immutable item; 0 .. bin.length)
|
2013-04-10 21:29:02 -07:00
|
|
|
sample = sofn(item);
|
2014-04-02 16:56:35 +00:00
|
|
|
foreach (immutable s; sample)
|
2013-04-10 21:29:02 -07:00
|
|
|
bin[s]++;
|
|
|
|
|
}
|
|
|
|
|
writefln("Item counts for %d runs:\n%s", nRuns, bin);
|
|
|
|
|
}
|