You should probably look up the binomial theorem but I always like
simulations anyway myself.  The numerous instances of ">:" here is for the
assumption that dice are numbered from 1 to n whereas J's random number
generator gives values from 0 to n-1.

   $>:1e5 5?@$8         NB. Table of 1e5 rolls of 5 8-sided dice
100000 5
   $+/"1 >:1e5 5?@$8    NB. Sum of each roll
100000
   $=+/"1 >:1e5 5?@$8   NB. (roughly) # possibs x # sums
36 100000
   possibs=. 5}.i.>:8*5 NB. Enumerate possiblities to be sure
   $=possibs,+/"1 >:1e5 5?@$8  NB. Table w/at least 1 entry/possib.
36 100036
   $<:+/"1]=possibs,+/"1 >:1e5 5?@$8  NB. Number of each possib,
36                                    NB. less the dummies we added.
   <:+/"1]=possibs,+/"1 >:1e5 5?@$8   NB. Example distribution.
3 11 41 112 253 386 634 1019 1516 2135 2919 3583 4498 5429 6116 6825 7278
7388 7487 7160 6722 6122 5400 4444 3713 2729 2071 1546 1049 642 355 241 104
51 12 6

NB. Table of roll total, # of instances:
   possibs,.<:+/"1]=possibs,+/"1 >:1e5 5?@$8
 5    1
 6   17
 7   44
 8  116
 9  240
10  411
11  656
...

On Tue, Oct 11, 2011 at 7:37 AM, Raul Miller <rauldmil...@gmail.com> wrote:

> On Tue, Oct 11, 2011 at 7:28 AM, David Vaughan
> <purpleblue...@googlemail.com> wrote:
> > Before you sent that, I was messing around with the power conjunction and
> came up with:
> >
> >   d1=. 1 2 3 4 5 6
> >   (+/%#)7=,d1(+/)^:1 d1
> > 0.166667
> >
> > Set n in ^:n to rolls-1.
> >
> > Which method, yours or mine do you think is the better one for speed and
> space?
>
> I would test to find out (perhaps with somewhat larger numbers, for
> measurement purposes).
>
> I would expect that yours is slightly better for space and that mine
> is slightly better for speed.  But I could easily be wrong
>
> --
> Raul
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>



-- 
Devon McCormick, CFA
^me^ at acm.
org is my
preferred e-mail
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to