1
$\begingroup$

I'm imagining the likes of this:

(* programmatically construct list of N formal params *)

(* somehow curry or bind this list to the first arg of Table[] *)

(* generate data list: {{f1,1,6}, {f2,1,6}, ... {fn,1,6} *)

(* apply my bound Table[] func to my data list, effectively giving:

    Table[{f1,f2,...fn}, {f1,1,6}, {f2,1,6}, ... {fn,1,6}]

*)

I'm playing around with a few different constructs, but I can't get anywhere – and I can't seem to describe this question in a search-friendly way.

The point is so I can do some investigation around the PDF and CDF curves you get when throwing N dice.

$\endgroup$
  • $\begingroup$ Not responsive to the primary question, but to the goal, perhaps you'll find this useful: die = Table[1/6, {6}]; numberRolled = 3; totpmf = Transpose[{Range[1*numberRolled, Length@die*numberRolled], Nest[ListConvolve[die, #, {1, -1}, 0] &, die, numberRolled - 1]}] $\endgroup$ – ciao Jul 17 '15 at 7:16
  • 1
    $\begingroup$ Tuples and Array might make it simpler than Table, depending on what you need precisely. Try Array[f, {4, 4, 4, 4}]. $\endgroup$ – Szabolcs Jul 17 '15 at 7:22
  • $\begingroup$ …and for simulation purposes, this is what RandomChoice[] was intended for. $\endgroup$ – J. M. will be back soon Jul 17 '15 at 7:24
  • $\begingroup$ Thanks folks ... I will read up & play around with these! $\endgroup$ – billc Jul 17 '15 at 7:40
  • $\begingroup$ Try also e.g. n = 2; m = 4; fn = #2^2 + #1 &; Array[fn, Table[m, {n}]]. $\endgroup$ – Mr.Wizard Jul 17 '15 at 7:56
2
$\begingroup$

I figured out a way to do what I was trying to do.

Turns out the graph is not that interesting b/c the scale explodes so quickly.

In anycase, I believe what I have made use of here (the xx[n] construct) is an "indexed object".

Manipulate[
 formals = Table[xx[n], {n, 1, a}];
 data = {#, 1, 6} & /@ formals;

 t = Table @@ Prepend[data, formals];

 pdf = Tally[Plus @@@ Flatten[t, a - 1]];
 cdf = Thread@{pdf[[All, 1]], Accumulate[pdf[[All, 2]]]};
 ListPlot[{pdf, cdf}, Filling -> Axis, ImageSize -> Large]
 , {a, 1, 5, 1}]

enter image description here

$\endgroup$
1
$\begingroup$

Here's an alternative take.

pdf[n_Integer] := Tally@Flatten@Array[Plus, ConstantArray[6, n]]
Manipulate[ListPlot[pdf[n]], {n, 1, 5, 1}]

Performance is significantly improved (10 dice is bearable).

To get the cdf add memoization (because bruteforce computation is costly, but end result is simple) and have this:

pdf[n_Integer] := (pdf[n] = 
   (Tally@Flatten@Array[Plus, ConstantArray[6, n]])~SortBy~First)
cdf[pdf_] := FoldList[{First@#2, Last@#1 + Last@#2} &, pdf]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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