For the fun of it.

d2=: 3 : 0

NB. t : total to add to
NB. m : maximum number allowed
NB. c : count of numbers to add up
't m c'=:y

NB. Reduce problem to start from zero to <:t
t=:t-c

NB. Upper Limit
ul=:{:<.t-+/

NB. Lower limit
ll=:[:>.(t-+/)%c-#

NB. numbers from [ to ]
x2y=:[+i.@>:@-~

NB. set starting numbers
n=:,.(ll '')x2y <:m

NB. Put it all together
>:(([:;([:<],"1 0 ll x2y ul)"1))^:(<:c)n
)

(#,[:~.+/"1) d2 172 70 6

1141667 172




On Wed, Jun 27, 2018 at 4:48 PM Skip Cave <s...@caveconsulting.com> wrote:

> Devon,
>
> That 0-1 fraction method is a really nice way to generate lots of sets of 6
> random integers 1-70 that sum to 172.
> That got me to thinking -- shouldn't we be able to make all the generated
> 6-integer sets sum to 172?
>
> f01=. 1e5 6?@$0 NB. Generating the fractions
>
> NB. Now if we instument your verb a bit more:
>
> itt1=. f01#~c=.172=+/"1 b=.([: <. 0.5 + ] * 172 % +/"1)"1 ] f01
>
> $itt1
>
> 55085 6
>
> NB. Not bad. Over half of the strings sum to 172.
>
>
> NB. What about the other half (invert the c selection vector)?
>
> 5{.b#~-.c
>
> 42 23 5 20 41 40
>
> 50 35 50 8 19 11
>
> 37 34 34 17 22 27
>
> 49 10 25 2 34 53
>
> 30 14 44 15 32 38
>
>
> How far off are the rest of these strings ?
>
> d=.172-+/"1 b#~-.-.c
>
> 20{.d
>
> 1 _1 1 _1 _1 _1 1 1 _1 1 1 _1 1 1 1 1 1 1 _1 1
>
> _20{.d
>
> 1 1 _1 _1 1 1 _1 _1 _1 _1 _1 1 _1 2 _2 1 1 _1 _1 1
>
>
> >./d
>
> 2
>
> <./d
>
> _2
>
> NB. Looks like the rest of the strings are off by as much as +/- 2
>
>
> So we should be able find the discrepancy in the remaining strings, and
> randomly pick one or more of the integers in each of the strings to add or
> subtract 1 or 2 to. Then all the strings will sum to 172.
>
>
> I'll leave that exercise to the reader...
>
>
> Skip
>
>
> Skip Cave
> Cave Consulting LLC
>
>
>
> On Wed, Jun 27, 2018 at 10:27 AM Devon McCormick <devon...@gmail.com>
> wrote:
>
> > You could do it this way:
> >
> >    itt=. 1e5 6?@$0     NB. Generate 6 columns of numbers between 0 and 1
> >    NB. Multiply by scaling factor to force sum to 172, then round and
> >    NB. select those that sum to 172 after rounding.
> >    itt=. itt#~172=+/"1 ([: <. 0.5 + ] * 172 % +/"1)"1 ] itt
> >    3{.itt=. <.0.5+itt*("1 0)172%+/"1 itt
> > 30 62  7 22 50  1
> > 35 32 24 34 27 20
> > 21 39  1 14 43 54
> >
> >    NB. Remove any row with element not less than or equal to 70:
> >    $itt=. itt#~*./"1 itt<:70
> > 52205 6
> >
> > So, over half of our random selections can be made to work.
> >
> > On Wed, Jun 27, 2018 at 2:24 AM, Skip Cave <s...@caveconsulting.com>
> > wrote:
> >
> > > Raul, Robert, Jimmy, Louis, Bo, Rob,
> > > Thanks so much for all the thoughtful and interesing answers. Your
> > examples
> > > gave me much insight on how the while. do. control structures work,
> > which I
> > > can never seem to remember.  I also got a better understanding of
> > > itemize/laminate, very useful for sticking vectors together.
> > >
> > > In my particular case. I didn't have to generate millions of
> > combinations,
> > > so efficiency wasn't much of an issue for me.
> > >
> > > Skip
> > >
> > >
> > > On Tue, Jun 26, 2018 at 9:12 AM Skip Cave <s...@caveconsulting.com>
> > wrote:
> > >
> > > > I want to generate n sets of 6 random integers from 1->70 where each
> > set
> > > > of 6 integers sums to 172
> > > >
> > > > Here's my first try:
> > > >
> > > > ts =: 3 : 0
> > > >
> > > > b=: 2 6$0
> > > >
> > > > for. i.y do. a=:1+6?70
> > > >
> > > > b=:b,a
> > > >
> > > > end.
> > > >
> > > > b#~172=+/"1 b
> > > >
> > > > )
> > > >
> > > > Test it:
> > > >
> > > > ts 1000
> > > >
> > > > 27 60 5 24 53 3
> > > >
> > > > 38 35 3 15 57 24
> > > >
> > > > 16 29 19 50 4 54
> > > >
> > > > This generates some 6-element vectors that sum to 172 by elimination,
> > but
> > > > I don't have control of how many vectors it produces.
> > > >
> > > >
> > > > I want to change the logic in the loop so that it keeps generating
> sets
> > > of
> > > > 6 random integers, testing whether they sum to 172, saving just the
> > sets
> > > > that sum to 172, until I have saved y random sets that sum to 172,
> and
> > > then
> > > > exits.
> > > >
> > > >
> > > > Any help would be appreciated.
> > > >
> > > >
> > > > Skip
> > > >
> > > >
> > > > Skip Cave
> > > > Cave Consulting LLC
> > > >
> > > ----------------------------------------------------------------------
> > > For information about J forums see http://www.jsoftware.com/forums.htm
> > >
> >
> >
> >
> > --
> >
> > Devon McCormick, CFA
> >
> > Quantitative Consultant
> > ----------------------------------------------------------------------
> > For information about J forums see http://www.jsoftware.com/forums.htm
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to