Anonymous user
Probabilistic choice: Difference between revisions
Updated to work with Nim 1.4. Heavy restructuring.
(Updated to work with Nim 1.4. Heavy restructuring.) |
|||
Line 1,982:
=={{header|Nim}}==
<lang nim>import tables,
const▼
precsn = 6▼
var start = cpuTime()
▲const
NumTrials = 1_000_000
Probabilities = {"aleph": 1 / 5, "beth": 1 / 6, "gimel": 1 / 7, "daleth": 1 / 8,
"he": 1 / 9, "waw": 1 / 10, "zayin": 1 / 11, "heth": 1759 / 27720}.toTable
▲var samples = initTable[string,int](16)
samples.add(i,0)▼
randomize()
for i in 1 .. num_trials:▼
var z = random(1.0)▼
if z < probs[j]:▼
break▼
else:▼
var s1, s2: float▼
for item, prob in Probabilities.pairs:
echo " Item Target Results Differences"
for item, prob in Probabilities.pairs:
let r = samples[item] / NumTrials
s1 += r * 100
echo &"{item:<6} {prob:.6f} {r:.6f} {100 * (1 - r / prob):9.6f} %"
echo &"Total: {s2:^8.2f} {s1:^8.2f}"
▲echo("==== ","\t","====== ","\t","======= ","\t","==========")
▲ s2 += probs[i]*100.0
▲echo("======","\t","======= ","\t","======== ")
▲echo("\n",formatFloat(cpuTime()-start,ffDecimal,2)," secs")</lang>
{{out}}
<pre> Item
======
he
heth
beth 0.166667 0.166386 0.168400%▼
aleph
▲zayin 0.090909 0.090923 -0.015300%
▲gimel 0.142857 0.142691 0.116300%
waw 0.100000 0.099650 0.350000 %
▲daleth 0.125000 0.124911 0.071200%
======
Total:
=={{header|OCaml}}==
|