Re: D&D Dice
- To: mathgroup at smc.vnet.net
- Subject: [mg37531] Re: [mg37457] D&D Dice
- From: Daniel Lichtblau <danl at wolfram.com>
- Date: Sun, 3 Nov 2002 02:59:05 -0500 (EST)
- References: <200210310940.EAA18264@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
AngleWyrm wrote: > > Consider yer basic 3d6 stat roll: What are the possibilities? > This gives a table of all 6^3 = 216 rolls: > > Table[i + i2 + i3, {i, 6}, {i2, 6}, {i3, 6}] // MatrixForm > > My question is this: How shall I set up a formula to list the count of each > total? One approach might be to mess with elements of (x[1]+x[2]+x[3]+x[4]+x[5]+x[6])^3. The code below will do this. I'm sure it can be made more efficient e.g. by cutting down on use of pattern matching. ddice[sides_,n_] := Module[ {x,sum,pow,ll1,ll2,ll3,ll4,ll5}, sum = Apply[Plus, Array[x,sides]]; pow = Expand[sum^n]; ll1 = Apply[List,pow]; ll2 = ll1 /. x[j_]^k_. -> ll[k*j]; ll3 = ll2 //. {ll[j_]*ll[k_] -> ll[j+k], ll[j_]^k_->ll[k*j]}; ll4 = ll3 /. a_.*ll[b_] -> {b,a}; ll5 = Split[Sort[ll4], #1[[1]]==#2[[1]]&]; Map[{#[[1,1]],Apply[Plus,Map[#[[2]]&,#]]}&, ll5] ] Example: In[17]:= ddice[6,3] Out[17]= {{3, 1}, {4, 3}, {5, 6}, {6, 10}, {7, 15}, {8, 21}, {9, 25}, {10, 27}, {11, 27}, {12, 25}, {13, 21}, {14, 15}, {15, 10}, {16, 6}, {17, 3}, {18, 1}} The efficiency is not terrible. It will handle 9 throws of dice with 10 sides in around 11 seconds on my machine. In[18]:= InputForm[Timing[ddice[10,9]]] Out[18]//InputForm= {10.76*Second, {{9, 1}, {10, 9}, {11, 45}, {12, 165}, {13, 495}, {14, 1287}, {15, 3003}, {16, 6435}, {17, 12870}, {18, 24310}, {19, 43749}, {20, 75501}, {21, 125565}, {22, 202005}, {23, 315315}, {24, 478731}, {25, 708444}, {26, 1023660}, {27, 1446445}, {28, 2001285}, {29, 2714319}, {30, 3612231}, {31, 4720815}, {32, 6063255}, {33, 7658190}, {34, 9517662}, {35, 11645073}, {36, 14033305}, {37, 16663185}, {38, 19502505}, {39, 22505751}, {40, 25614639}, {41, 28759500}, {42, 31861500}, {43, 34835625}, {44, 37594305}, {45, 40051495}, {46, 42126975}, {47, 43750575}, {48, 44865975}, {49, 45433800}, {50, 45433800}, {51, 44865975}, {52, 43750575}, {53, 42126975}, {54, 40051495}, {55, 37594305}, {56, 34835625}, {57, 31861500}, {58, 28759500}, {59, 25614639}, {60, 22505751}, {61, 19502505}, {62, 16663185}, {63, 14033305}, {64, 11645073}, {65, 9517662}, {66, 7658190}, {67, 6063255}, {68, 4720815}, {69, 3612231}, {70, 2714319}, {71, 2001285}, {72, 1446445}, {73, 1023660}, {74, 708444}, {75, 478731}, {76, 315315}, {77, 202005}, {78, 125565}, {79, 75501}, {80, 43749}, {81, 24310}, {82, 12870}, {83, 6435}, {84, 3003}, {85, 1287}, {86, 495}, {87, 165}, {88, 45}, {89, 9}, {90, 1}}} I wonder if there are faster direct approaches using e.g. multinomial coefficients? Daniel Lichtblau Wolfram Research