Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
30
Task/Knuths-algorithm-S/D/knuths-algorithm-s-1.d
Normal file
30
Task/Knuths-algorithm-S/D/knuths-algorithm-s-1.d
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
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;
|
||||
else if (uniform01 < (double(n) / i))
|
||||
sample[uniform(0, n)] = item;
|
||||
return sample;
|
||||
};
|
||||
}
|
||||
|
||||
void main() {
|
||||
enum nRuns = 100_000;
|
||||
size_t[10] bin;
|
||||
|
||||
foreach (immutable trial; 0 .. nRuns) {
|
||||
immutable sofn = sofN_creator(3);
|
||||
int[] sample;
|
||||
foreach (immutable item; 0 .. bin.length)
|
||||
sample = sofn(item);
|
||||
foreach (immutable s; sample)
|
||||
bin[s]++;
|
||||
}
|
||||
writefln("Item counts for %d runs:\n%s", nRuns, bin);
|
||||
}
|
||||
30
Task/Knuths-algorithm-S/D/knuths-algorithm-s-2.d
Normal file
30
Task/Knuths-algorithm-S/D/knuths-algorithm-s-2.d
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
import std.stdio, std.random, std.algorithm;
|
||||
|
||||
struct SOfN(size_t n) {
|
||||
size_t i;
|
||||
int[n] sample = void;
|
||||
|
||||
int[] next(in size_t item, ref Xorshift rng) {
|
||||
i++;
|
||||
if (i <= n)
|
||||
sample[i - 1] = item;
|
||||
else if (rng.uniform01 < (double(n) / i))
|
||||
sample[uniform(0, n, rng)] = item;
|
||||
return sample[0 .. min(i, $)];
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
enum nRuns = 100_000;
|
||||
size_t[10] bin;
|
||||
auto rng = Xorshift(0);
|
||||
|
||||
foreach (immutable trial; 0 .. nRuns) {
|
||||
SOfN!3 sofn;
|
||||
foreach (immutable item; 0 .. bin.length - 1)
|
||||
sofn.next(item, rng);
|
||||
foreach (immutable s; sofn.next(bin.length - 1, rng))
|
||||
bin[s]++;
|
||||
}
|
||||
writefln("Item counts for %d runs:\n%s", nRuns, bin);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue