Dice game probabilities: Difference between revisions

Content added Content deleted
m (→‎tbd: fixed for classic Rexx)
(→‎{{header|J}}: formatting)
Line 156: Line 156:


=={{header|J}}==
=={{header|J}}==
'''Solution:'''
<lang J>
gen_dict =: (({. , #)/.~@:,@:(+/&>)@:{@:(# <@:>:@:i.)~ ; ^)&x:
<lang J>gen_dict =: (({. , #)/.~@:,@:(+/&>)@:{@:(# <@:>:@:i.)~ ; ^)&x:


beating_probability =: 4 : 0
beating_probability =: dyad define
'C0 P0' =. gen_dict/ x
'C0 P0' =. gen_dict/ x
'C1 P1' =. gen_dict/ y
'C1 P1' =. gen_dict/ y
(C0 +/@:,@:(>/&:({."1) * */&:({:"1)) C1) % (P0 * P1)
(C0 +/@:,@:(>/&:({."1) * */&:({:"1)) C1) % (P0 * P1)
)</lang>
)
'''Example Usage:'''
10 5 (;x:inv)@:beating_probability 7 6
<lang J> 10 5 (;x:inv)@:beating_probability 7 6
┌─────────────────────┬────────┐
┌─────────────────────┬────────┐
│3781171969r5882450000│0.642789│
│3781171969r5882450000│0.642789│
Line 172: Line 172:
┌─────────────────┬────────┐
┌─────────────────┬────────┐
│48679795r84934656│0.573144│
│48679795r84934656│0.573144│
└─────────────────┴────────┘
└─────────────────┴────────┘</lang>
</lang>
gen_dict explanation:<br>
gen_dict explanation:<br>
gen_dict is akin to gen_dict in the python solution and
<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.
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 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.
the second column is the number of ways this many pips can occur.


({. , #)/.~ make a vector having items head and tally of each group of like items in this case pip count and occurrences<br>
<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 of the<br>
<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>
<code>; ^</code> 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 beating_probability is akin to the python solution function having same name.<br>
The verb <code>beating_probability</code> is akin to the python solution function having same name.<br>
C0 >/&:({."1) C1 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 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>
C0 */&:({:"1) C1 is the corresponding table of occurrences<br>
<code>C0 */&:({:"1) C1</code> is the corresponding table of occurrences<br>
* naturally we multiply the two tables (atom by atom, not a matrix product)<br>
<code>*</code> naturally we multiply the two tables (atom by atom, not a matrix product)<br>
+/@:,@: sum the raveled table<br>
<code>+/@:,@:</code> sum the raveled table<br>
% (P0 * P1) after which divide by the all possible rolls.
<code>% (P0 * P1)</code> after which divide by the all possible rolls.


=={{header|Java}}==
=={{header|Java}}==