Hi,
I'm new to J (although I've been interested in it for many years, back
to J 6 or so!). I've not really gone through many of the tutorials, as
I've got a specific issue that I think might be a good "fit" for J,
but I'm not sure how to proceed.

I have a function that generates (random) results, for example the
following that rolls N dice and adds the best 3 results:

   best3ofN =. +/ @: (3&{.) @: \:~ @: >: @: ? @: #
   5 best3ofN 6
16

What I want to do is to run that function a large number of times, and
"tabulate" the results - so I'll get a 2-d array of result-count
pairs.

(My ultimate goal is to have a function that takes any function
similar to "best3ofN" and produces a result array for it - so "1000000
histogram sum5d6" would display the results of running "sum5d6" a
million times, for example).

The simplest way of doing this is just to get an array of the results,
then tally that up (I say "simplest", but it took me quite a while to
work out how to do this :-)). But that runs out of memory fairly
quickly, so what I'd prefer to do is to generate the results one by
one, and maintain a cumulative total. I suspect I should be able to do
this with some creative use of ^:, but I can't work out how. It's
possible that my frustration comes from the fact that in effect I have
a "non-pure" function, that takes no inputs and returns different
values each time - that seems like it's not a good fit for J's model.
But I'm sure that's more a failure of my imagination than of J itself.

Any pointers on how to solve this problem would be much appreciated. I
realise that "go and read the tutorials, learn how to think in J, and
then it'll all be clear" is probably the best advice, but I'd like to
prove to myself that solving the sort of real-world problems I'm
interested in is possible/practical before doing a lot of tutorial
work.

Thanks,
Paul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to