I should learn to read the specs, huh Raul?

Based on Raul's better submission. (I liked the use of #~ to select appropriate 
candidates from ,: items)

Cheers, bob

ts =: 3 : 0

  b=: 0 6$0

  while. y>#b do. b=. b, (#~ 172=+/"1) a=.,:1+6?70 end.  

)

   ts 6
29 49 17 47 22  8
 8 43 39 40  7 35
27 39 30 38 13 25
17 27 26 56 12 34
40 58 16 41 10  7
22 41 64  7 14 24

   ts 8
31 12  1 43 60 25
 1 39 69 24 16 23
 7 31 37 43 46  8
28  5 47 61  1 30
48 25 24 12 19 44
23  8 53 40  2 46
37  2 28 34 35 36
37 47 14 26 45  3
   
> On Jun 26, 2018, at 7:58 AM, robert therriault <bobtherria...@mac.com> wrote:
> 
> Hi Skip,
> 
> I came up with this using while. and putting the test for summation inside 
> the loop.
> 
> Cheers, bob
> 
> ts =: 3 : 0
> 
> b=: 0 6$0
> 
> while. 6>#b do. a=:1+6?70
>             if. 172=+/"1 a do. b=:a,b end.
>             end.
> b
> 
> )
> 
>   ts ''
> 10 60 13 26 14 49
> 6 37 65  5 43 16
> 23 26 11 56 51  5
> 59 19 22 62  9  1
> 26 69 30 24 21  2
> 12 36 16 17 63 28
>   ts ''
> 48  5 43 35 19 22
> 35 32 53 43  3  6
> 2 32  6 62 36 34
> 33 37 36 10 17 39
> 1 22 53  3 32 61
> 19 15 66 42 21  9
> 
> 
>> On Jun 26, 2018, at 7: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
> 
> ----------------------------------------------------------------------
> 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