How to resolve the algorithm Dice game probabilities step by step in the Wren programming language
How to resolve the algorithm Dice game probabilities step by step in the Wren 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 Wren programming language
Source code in the wren programming language
var throwDie // recursive
throwDie = Fn.new { |nSides, nDice, s, counts|
if (nDice == 0) {
counts[s] = counts[s] + 1
return
}
for (i in 1..nSides) throwDie.call(nSides, nDice-1, s + i, counts)
}
var beatingProbability = Fn.new { |nSides1, nDice1, nSides2, nDice2|
var len1 = (nSides1 + 1) * nDice1
var c1 = List.filled(len1, 0)
throwDie.call(nSides1, nDice1, 0, c1)
var len2 = (nSides2 + 1) * nDice2
var c2 = List.filled(len2, 0)
throwDie.call(nSides2, nDice2, 0, c2)
var p12 = nSides1.pow(nDice1) * nSides2.pow(nDice2)
var tot = 0
for (i in 0...len1) {
for (j in 0...i.min(len2)) {
tot = tot + c1[i] * c2[j] / p12
}
}
return tot
}
System.print(beatingProbability.call(4, 9, 6, 6))
System.print(beatingProbability.call(10, 5, 7, 6))
You may also check:How to resolve the algorithm Polymorphism step by step in the Python programming language
You may also check:How to resolve the algorithm LZW compression step by step in the Arturo programming language
You may also check:How to resolve the algorithm Non-decimal radices/Output step by step in the MATLAB / Octave programming language
You may also check:How to resolve the algorithm Babbage problem step by step in the Smalltalk programming language
You may also check:How to resolve the algorithm Sorting algorithms/Bubble sort step by step in the Draco programming language