tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
15
Task/Probabilistic-choice/D/probabilistic-choice-1.d
Normal file
15
Task/Probabilistic-choice/D/probabilistic-choice-1.d
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
import std.stdio, std.random, std.string, std.range;
|
||||
|
||||
void main() {
|
||||
enum int nTrials = 1_000_000;
|
||||
enum items = "aleph beth gimel daleth he waw zayin heth".split();
|
||||
enum pr = [1/5., 1/6., 1/7., 1/8., 1/9., 1/10., 1/11., 1759/27720.];
|
||||
|
||||
double[pr.length] counts = 0.0;
|
||||
foreach (_; 0 .. nTrials)
|
||||
counts[dice(pr)]++;
|
||||
|
||||
writeln("Item Target prob Attained prob");
|
||||
foreach (name, p, co; zip(items, pr, counts[]))
|
||||
writefln("%-7s %.8f %.8f", name, p, co / nTrials);
|
||||
}
|
||||
24
Task/Probabilistic-choice/D/probabilistic-choice-2.d
Normal file
24
Task/Probabilistic-choice/D/probabilistic-choice-2.d
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import std.stdio, std.random, std.algorithm, std.range;
|
||||
|
||||
void main() {
|
||||
enum int nTrials = 1_000_000;
|
||||
auto items = "aleph beth gimel daleth he waw zayin heth".split();
|
||||
enum pr = [1/5., 1/6., 1/7., 1/8., 1/9., 1/10., 1/11., 1759/27720.];
|
||||
|
||||
double[pr.length] cumulatives = pr[];
|
||||
foreach (i, ref c; cumulatives[1 .. $ - 1])
|
||||
c += cumulatives[i];
|
||||
cumulatives[$ - 1] = 1.0;
|
||||
|
||||
double[pr.length] counts = 0.0;
|
||||
auto rnd = Xorshift(unpredictableSeed());
|
||||
foreach (_; 0 .. nTrials) {
|
||||
double rnd01 = rnd.front / cast(double)rnd.max;
|
||||
rnd.popFront();
|
||||
counts[cumulatives[].countUntil!(c => c >= rnd01)()]++;
|
||||
}
|
||||
|
||||
writeln("Item Target prob Attained prob");
|
||||
foreach (name, p, co; zip(items, pr, counts[]))
|
||||
writefln("%-7s %.8f %.8f", name, p, co / nTrials);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue