1
$\begingroup$

I'm trying to show the possible combinations of rolling 3 dice and taking the sum of the two highest numbers

dice = {1, 2, 3, 4, 5, 6}
j = Flatten[Outer[List, dice, dice, dice], 2]
j1 = Table[Extract[j, {i, 2}], {i, 1, 216}];
j2 = Table[Extract[j, {i, 3}], {i, 1, 216}];
Transpose[{j1, j2}];

How can I add the two elements up of each part of the last line (Transpose...) to give the sums ranging from 2 to 12? Thank you

$\endgroup$
3
  • 4
    $\begingroup$ Why not use Tuples[]? Total[Rest[#]] & /@ Tuples[Range[6], 3] $\endgroup$ Commented Jan 27, 2020 at 12:04
  • 1
    $\begingroup$ Just to sum up the sublists use:Total[#] & /@ Transpose[{j1, j2}] $\endgroup$
    – RMMA
    Commented Jan 27, 2020 at 12:58
  • 1
    $\begingroup$ @RMMA - The mapping can be simplified to Total /@ Transpose[{j1, j2}] $\endgroup$
    – Bob Hanlon
    Commented Jan 27, 2020 at 15:18

1 Answer 1

1
$\begingroup$

You can simply add your j1 and j2:

rowtotals  = j1 + j2;

Total[Transpose[{j1, j2}], {2}] == rowtotals

True

Note that you can use {2} in the second argument of Total to get row totals.

You can get the same result using a combination of Tuples and Total as suggested by J.M. in comments:

Total[Tuples[Range @ 6, 3][[All, 2 ;;]], {2}] == rowtotals

True

ListLinePlot[rowtotals]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.