September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
22
Task/Probabilistic-choice/Zkl/probabilistic-choice.zkl
Normal file
22
Task/Probabilistic-choice/Zkl/probabilistic-choice.zkl
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
var names=T("aleph", "beth", "gimel", "daleth",
|
||||
"he", "waw", "zayin", "heth");
|
||||
var ptable=T(5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0).apply('/.fp(1.0));
|
||||
ptable=ptable.append(1.0-ptable.sum(0.0)); // add last weight to sum to 1.0
|
||||
var [const] N=ptable.len();
|
||||
|
||||
fcn ridx{ i:=0; s:=(0.0).random(1);
|
||||
while((s-=ptable[i]) > 0) { i+=1 }
|
||||
i
|
||||
}
|
||||
|
||||
const M=0d1_000_000;
|
||||
var r=(0).pump(N,List,T(Ref,0)); // list of references to int 0
|
||||
(0).pump(M,Void,fcn{r[ridx()].inc()}); // 1,000,000 weighted random #s
|
||||
|
||||
r=r.apply("value").apply("toFloat"); // (reference to int)-->int-->float
|
||||
|
||||
println(" Name Count Ratio Expected");
|
||||
foreach i in (N){
|
||||
"%6s%7d %7.4f%% %7.4f%%".fmt(names[i], r[i], r[i]/M*100,
|
||||
ptable[i]*100).println();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue