How to resolve the algorithm Dice game probabilities step by step in the zkl programming language
How to resolve the algorithm Dice game probabilities step by step in the zkl programming language
Table of Contents
Problem Statement
Two players have a set of dice each. The first player has nine dice with four faces each, with numbers one to four. The second player has six normal dice with six faces each, each face has the usual numbers from one to six. They roll their dice and sum the totals of the faces. The player with the highest total wins (it's a draw if the totals are the same). What's the probability of the first player beating the second player? Later the two players use a different set of dice each. Now the first player has five dice with ten faces each, and the second player has six dice with seven faces each. Now what's the probability of the first player beating the second player? This task was adapted from the Project Euler Problem n.205: https://projecteuler.net/problem=205
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Dice game probabilities step by step in the zkl programming language
Source code in the zkl programming language
fcn combos(sides, n){
if(not n) return(T(1));
ret:=((0).max(sides)*n + 1).pump(List(),0);
foreach i,v in (combos(sides, n - 1).enumerate()){
if(not v) continue;
foreach s in (sides){ ret[i + s] += v }
}
ret
}
fcn winning(sides1,n1, sides2,n2){
p1, p2 := combos(sides1, n1), combos(sides2, n2);
win,loss,tie := 0,0,0; # 'win' is 1 beating 2
foreach i,x1 in (p1.enumerate()){
# using accumulated sum on p2 could save some time
win += x1*p2[0,i].sum(0);
tie += x1*p2[i,1].sum(0); // i>p2.len() but p2[bigi,?]-->[]
loss+= x1*p2[i+1,*].sum(0);
}
s := p1.sum(0)*p2.sum(0);
return(win.toFloat()/s, tie.toFloat()/s, loss.toFloat()/s);
}
println(winning([1..4].walk(), 9, [1..6].walk(),6));
println(winning([1..10].walk(),5, [1..7].walk(),6)); # this seem hardly fair
You may also check:How to resolve the algorithm Documentation step by step in the 6502 Assembly programming language
You may also check:How to resolve the algorithm Currying step by step in the Nemerle programming language
You may also check:How to resolve the algorithm Determine if a string is numeric step by step in the Wren programming language
You may also check:How to resolve the algorithm Quine step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm XML/XPath step by step in the XPL0 programming language