Skip, if the goal is to write the code then the responses have already advised best ways.
But if you require very high performance (eg in Monte Carlo simulations ?), then you can always consider a trade off or speed vs space. For example if you have space available, and runtime CPU is critical, you could also use brute force (in this example, but not always possible, for example the problem explodes if you use 100 integers). For brute force I used the comb and combM verbs at this link: https://code.jsoftware.com/wiki/User:RE_Boss/Performance_Records/Combinations <https://code.jsoftware.com/wiki/User:RE_Boss/Performance_Records/Combinations> $s1=:>:6 combM 70 NB. Generate all sets of 6, using numbers i.70 (use >: to convert to 1 ... 70) 131115985 6 b1=:172=+/"1 s1 NB. Boolean list where sets sum to 172 $s2=:b1#s1 NB. This is now your whole solution space 763076 6 9!:11 ]15 NB. print precision to 15 significant figures 7!:5 < ‘s1’ NB. entire space required 8.58GB for the 131m rows 8589934592 7!:5 < ‘s2’ NB. solution space is 67MB for the 763k rows 67108864 Now you can simply randomly query and index to produce sample sets, very fast. s2 {~ ?{.$s2 NB. Randomly generate (select) a set 10 12 19 26 44 61 s2 {~ 4?{.$s2 NB. Without replacement (ie no repeating sets) 3 12 14 40 49 54 11 20 21 33 37 50 4 5 10 33 56 64 9 13 23 32 42 53 The above took in total about 50 seconds to run on my MacBookPro with 16GB, but once computed produces (selects) the random sets instantly. Hope is useful …/Rob > On 27 Jun 2018, at 3:39 am, 'Bo Jacoby' via Programming > <programm...@jsoftware.com> wrote: > > 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 ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm