Dice game probabilities: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 1,287:
computed: p1 = 5d10, p2 = 6d7, prob = 7562343938 / 11764900000 = 0.64278862871763
</pre>
 
=={{header|M2000 Interpreter}}==
Using a buffer of random numbers for all games.
 
 
<syntaxhighlight lang="m2000 interpreter">
Declare random1 lib "advapi32.SystemFunction036" {long lpbuffer, long length}
Flush
Prob(4, 9, 6, 6, 55555)
Prob(10, 5, 7, 6, 55555)
Sub Prob(face1 as long=4 ,dice1=9, face2 as long=6,dice2=6, games=1000)
profiler
if face1<1 or face1>256 or face2<1 or face2>256 then Error "Faces out of limits"
local m=@PlayAll(dice1,dice2, games) : local tt=timecount
print "Time to fill the buffer with random bytes ";str$(tt, "#0.00 ms")
local s1=dice1-1, s2=dice2-1, k, l, t, f1=face1/256, f2=face2/256, i, n1=0&, n2=0&
print "Buffer size ";str$(len(m)/1024,"#0.#");" Kbyte": Refresh
profiler
for i=0 to games-1
long n1=dice1:for j=0to s1{n1+=eval(m, i!n1!j)*f1}
long n2=dice2:for j=0to s2{n2+=eval(m, i!n2!j)*f2}
if n1>n2 then k++ else if n1<n2 then l++
next
t=games-k-l
print "Games Total "; games
print "Player 1 wins ";k; " probability of winning:" ;str$(k/games, "#0.00 %")
print "Player 2 wins ";l; " probability of winning:" ;str$(l/games, "#0.00 %")
print "Number of ties ";t
print "Execution time ";str$(timecount/1000, "#0.0 s")
End Sub
Function PlayAll(dice1, dice2, games as long)
if games<1 then error "games<1"
local onegame
structure onegame {
n1 as byte * dice1
n2 as byte * dice2
}
buffer Alfa as onegame*games
call void random1(alfa(0), len(alfa))
= Alfa
End Function
</syntaxhighlight>
{{out}}
<pre>
Time to fill the buffer with random bytes 2.00 ms
Buffer size 813.8 Kbyte
Games Total 55555
Player 1 wins 31870 probability of winning:57.37 %
Player 2 wins 19729 probability of winning:35.51 %
Number of ties 3956
Execution time 179.9 s
 
Time to fill the buffer with random bytes 1.65 ms
Buffer size 596.8 Kbyte
Games Total 555555
Player 1 wins 35441 probability of winning:63.79 %
Player 2 wins 17605 probability of winning:31.69
Number of ties 2509
Execution time 138.7 s
</pre>
 
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
404

edits