Jump to content

Dice game probabilities: Difference between revisions

→‎{{header|J}}: formatting
m (→‎tbd: fixed for classic Rexx)
(→‎{{header|J}}: formatting)
Line 156:
 
=={{header|J}}==
'''Solution:'''
<lang J>
<lang J>gen_dict =: (({. , #)/.~@:,@:(+/&>)@:{@:(# <@:>:@:i.)~ ; ^)&x:
 
beating_probability =: 4 :dyad 0define
'C0 P0' =. gen_dict/ x
'C1 P1' =. gen_dict/ y
(C0 +/@:,@:(>/&:({."1) * */&:({:"1)) C1) % (P0 * P1)
)</lang J>
)
'''Example Usage:'''
<lang J> 10 5 (;x:inv)@:beating_probability 7 6
┌─────────────────────┬────────┐
│3781171969r5882450000│0.642789│
Line 172:
┌─────────────────┬────────┐
│48679795r84934656│0.573144│
└─────────────────┴────────┘</lang>
</lang>
gen_dict explanation:<br>
<code>gen_dict</code> is akin to <code>gen_dict</code> in the python solution and
returns a table and total number of combinations, order matters.
The table has 2 columns. The first column is the pip count on all dice,
the second column is the number of ways this many pips can occur.
 
<code>({. , #)/.~ </code> make a vector having items head and tally of each group of like items in this case pip count and occurrences operating on<br>
<code>,</code> raveled data (a vector) made of<br>
operating on<br>
<code>+/&></code> the sum of each of the<br>
, raveled data (a vector)<br>
made<code>{</code> Cartesian products of the<br>
<code>(# <@:>:@:i.)</code> equi-probable die values<br>
+/&> the sum of each<br>
<code>&x:</code> but first use extended integers<br>
of the<br>
<nowikicode>; ^</nowikicode> links the total possibilities to the result.
{ Cartesian products<br>
of the<br>
(# <@:>:@:i.) equi-probable die values<br>
&x: but first use extended integers<br>
<nowiki>; ^</nowiki> links the total possibilities to the result.
 
 
The verb <code>beating_probability</code> is akin to the python solution function having same name.<br>
<code>C0 >/&:({."1) C1</code> is a binary table where the pips of first player exceed pips of second player. "Make a greater than table but first take the head of each item."<br>
<code>C0 */&:({:"1) C1</code> is the corresponding table of occurrences<br>
<code>*</code> naturally we multiply the two tables (atom by atom, not a matrix product)<br>
<code>+/@:,@: </code> sum the raveled table<br>
<code>% (P0 * P1)</code> after which divide by the all possible rolls.
 
=={{header|Java}}==
892

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.