Make sure that the sum is 172. 
   (,~172-+/)>:?5#70 15 9 64 21 20 43
Discard results where the first item is not between 1 and 70

   (,~172-+/)>:?5#70
_31 67 61 41 12 22

This must be faster than discarding sets where the sum is not 172.
Thanks, 
Bo.

 

    Den 19:29 tirsdag den 26. juni 2018 skrev Louis de Forcrand 
<ol...@bluewin.ch>:
 

 Hi,

If I understand your wording, you would like to generate n “multisets” of 6 
random numbers which sum to 172. I say multiset because I imagine that order 
doesn’t matter, but that multiplicity does (so numbers can be repeated).

If speed is a concern, then you could generate all possible multisets and then 
select some at random. This takes some memory though, but I can run it on my 
phone so it shouldn’t be too bad. What follows was thrown together quickly, and 
could probably be improved.

g=: 4 : 0
 'm M'=. x
 'n s'=. y
 if. n<:0 do. i.1 0 return. end.
 min=. m >. s - M * d=. <:n
 max=. M <. s <.@% n
 p=. min ivl max
 ; (,"_ 1 ,&M g d , s&-)&.> p
)

ivl=: [ + 0 i.@>. >:@-~
$t=: 1 70 g 6 172  NB. 1141667*6 integers!
sel=: t {~ ?@$&(#t)

(m,M) g n,s generates all increasing sequences of n integers which sum to s, 
all between m and M inclusive (provided I didn’t make any mistakes!).
If what you want is actual sets, then g can be modified slightly to yield 
strictly increasing sequences, and the space required will be slightly lessened.

Cheers,
Louis

> On 26 Jun 2018, at 16:12, 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