If you don't need zeros, the solution is a bit easier: just take out the changes I made (add N to Q and subtract 1 from the result) to ensure zeros are possible!
There was a slight mistake in my algorithm besides that--the first number had a minimum that was one too small. So you have to use _1 instead of 0 as the element to be put in front of the random numbers, and to make sure the sum is correct, decrease Q by 1. The solution: N =: 10 Q =: 100 V =: N (2 -~/\ _1 , /:~@:? , ])&<: Q (+/ , #) V 100 10 V 5 1 21 5 8 3 17 24 7 9 Bonus: a way to enumerate the possible outcomes. Since dyadic ? picks one of the rows of comb with equal probability, the correctness of this function proves that the random function works. require 'stats/base' 3 (] (2 -~/\ _1 , /:~@] , [)"_1 comb)&<: 7 1 1 5 1 2 4 1 3 3 1 4 2 1 5 1 2 1 4 2 2 3 2 3 2 2 4 1 3 1 3 3 2 2 3 3 1 4 1 2 4 2 1 5 1 1 Marshall On Fri, May 08, 2015 at 02:40:38PM -0400, Dan Bron wrote: > Joe Bogner wrote: > > +/ every ( (([: /:~ [: ? N&#) I. i.) </. #&1) Q > > Yes, I. does seem germane to me, but I tried this verb and it seemed to > produce vectors of different lengths at each iteration. I need the result > to be exactly and always N numbers long, and their sum to be exactly and > always Q . > > Marshall wrote: > > The interpretation of the original problem that I would use is: > > V is a vector of nonnegative integers with shape N whose sum is Q. > > Thanks Marshall. The actual specification is > > V is a vector of POSITIVE integers with shape N whose sum is Q. > > That is: there must be exactly N buckets, and no bucket may be empty. All > possible ordered series of buckets must be equally likely. > > Pascal wrote: > > Maybe show some valid vectors for small Q? > > For example [1], for N=3 and Q=7 > > 1 1 5 > 1 2 4 > 1 3 3 > 1 4 2 > 1 5 1 > 2 1 4 > 2 2 3 > 2 3 2 > 2 4 1 > 3 1 3 > 3 2 2 > 3 3 1 > 4 1 2 > 4 2 1 > 5 1 1 > > A very regular pattern, now that I look at it. If we knew the number ZFNP > of possible 0-free N-partitions an integer Q, then we'd only have to roll > ?ZFNP and then figure out some function analogous to #: to break it apart > into its constituents. > > REB wrote: > > V=: Q*(%+/) 2 rnd N ?@# 0 > > This looks promising. What's rnd here? > > Bob wrote: > > Probably a converging power conjunction > > Ideally, I'm looking for an instantaneous / closed form solution, one which > avoids iteration or recurision, if possible. > > -Dan > > [1] I created this table by hand, but it looks right. > > > > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
