RosettaCodeData/Task/Probabilistic-choice/D/probabilistic-choice-1.d

16 lines
496 B
D
Raw Permalink Normal View History

2013-04-10 23:57:08 -07:00
void main() {
2014-04-02 16:56:35 +00:00
import std.stdio, std.random, std.string, std.range;
2013-04-10 23:57:08 -07:00
enum int nTrials = 1_000_000;
2014-04-02 16:56:35 +00:00
const items = "aleph beth gimel daleth he waw zayin heth".split;
const pr = [1/5., 1/6., 1/7., 1/8., 1/9., 1/10., 1/11., 1759/27720.];
2013-04-10 23:57:08 -07:00
double[pr.length] counts = 0.0;
2014-04-02 16:56:35 +00:00
foreach (immutable _; 0 .. nTrials)
counts[pr.dice]++;
2013-04-10 23:57:08 -07:00
writeln("Item Target prob Attained prob");
foreach (name, p, co; zip(items, pr, counts[]))
writefln("%-7s %.8f %.8f", name, p, co / nTrials);
}