deal=: 5 ? 52 #~ ] Let's say you want to do 4 deals at a time:
deal 4 27 44 10 41 40 9 14 48 28 44 42 7 31 27 11 9 20 42 44 10 3 repetitions of 4 deals at a time: deal"0 ]3$4 30 41 47 36 16 3 12 27 11 43 24 42 35 0 5 19 0 41 34 8 18 13 30 41 32 23 8 29 20 17 11 22 49 18 35 45 37 5 35 51 42 41 33 15 6 24 18 40 21 20 32 28 1 4 7 27 4 13 38 12 Suppose the statistics you want to calculate is "average": avg=: +/ % # [EMAIL PROTECTED]"0 ] 3$4 18.25 39 14.75 29 23.75 19.5 26.75 26 27.5 42 24 27.75 30 19 30.5 100 repetitions of one million deals at a time, calculating of average of each repetition: [EMAIL PROTECTED]"0 ] 100$1e6 25.5006 25.5132 25.4944 25.5042 25.4847 25.5046 25.4968 25.4929 25.5101 25.4952 25.5099 25.4909 25.4984 25.5091 25.5041 25.5058 25.5113 25.505 25.5203 25.4969 25.5065 25.4932 25.499 25.492 25.4825 ... ----- Original Message ----- From: Paul Moore <[EMAIL PROTECTED]> Date: Friday, October 3, 2008 4:08 Subject: [Jprogramming] Repeating a non-deterministic calculation To: J Programming <[email protected]> > I'm trying to run a simulation, where I generate a series of states > (deals of a hand of cards, series of dierolls, etc) and then calculate > stats for that series (i.e. a histogram of "scores", where the score > is calculated by a user-defined function). This works fine when > I can > generate an array of all the states at once - something like > > 5 ? (1000000 # 52) > > for a million 5-card deals from a deck of cards. But it runs out of > memory when I start getting to tens or hundreds of millions of > samples. Whet I'd like to do in that situation is to run the > calculations in batches of (say) a million at a time, and accumulate > the results. But what I can't see is an obvious way of saying "run > this calculation 10 times, regenerating the random numbers each time" > without using an explicit loop. > > To be explicit, a similar but simpler problem would be to > generate six > deals of 5 cards, by generating 3 deals twice: > > (5?3#52), (5?3#52) > 28 7 24 39 6 > 46 36 0 26 1 > 14 23 26 47 50 > 30 26 7 5 16 > 47 8 23 34 26 > 31 35 40 50 32 > > Is there a way of writing this where I can abstract (5?3#52) > out, and > only write it once? Something like "two_sets_of (5?3#52)" for some > defined sets_of? The simple definition > > two_sets_of =: monad : 'y, y' > > won't work because the same set of 3 rolls is used twice, rather than > 2 independent sets of rolls. ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
