Dice game probabilities: Difference between revisions

Content added Content deleted
(→‎{{header|J}}: formatting)
(Added Julia language)
Line 266: Line 266:
1000000 rolls, p1 = 5d10, p2 = 6d7
1000000 rolls, p1 = 5d10, p2 = 6d7
p1 wins 64.279% of the time</pre>
p1 wins 64.279% of the time</pre>

=={{header|Julia}}==
<lang julia>play(ndices::Int, nfaces::Int)::Int = nfaces * ndices == 0 ? 0 : sum(rand(1:nfaces) for i in 1:ndices)

function simulate(d1::Int, f1::Int, d2::Int, f2::Int; nrep::Int=1000000)::Float64
sum = 0
for i in 1:nrep
if play(d1, f1) > play(d2, f2)
sum += 1
end
end
return sum / nrep
end

println("\nPlayer 1: 9 dices, 4 faces\nPlayer 2: 6 dices, 6 faces\nP(Player1 wins) = ", simulate(9, 4, 6, 6))
println("\nPlayer 1: 5 dices, 10 faces\nPlayer 2: 6 dices, 7 faces\nP(Player1 wins) = ", simulate(5, 10, 6, 7))</lang>

{{out}}
<pre>Player 1: 9 dices, 4 faces
Player 2: 6 dices, 6 faces
P(Player1 wins) = 0.573187

Player 1: 5 dices, 10 faces
Player 2: 6 dices, 7 faces
P(Player1 wins) = 0.642336</pre>


=={{header|Kotlin}}==
=={{header|Kotlin}}==