Assigning two items together from an array Objectscript/Mumps

408 views Asked by At

Just wanted to ask whether there was a way to put together two items so for instance if I was generating 4 random shopping lists from a large list of 20 items which are all stored in my global(array) if I could divide this by 4 and randomly put together 4 smaller lists I know In c# I could possibly use the 'random' library is there anything similar to this in Mumps objectscript?

2

There are 2 answers

2
TIMOOOO On

Would $RANDOM(n) (or $R) help? It generates a random number between 0 and n.

0
trurl On

There are a dozen ways to do this, here's an example:

 RLIST
  N ITMS,I,FLG,R,ND,RES
  F I=1:1:20 S ITMS(I)="Item "_$E(I+100,2,3)  ; generate a list of items
  S ND="RES" K @ND
  ;
  F I=1:1:20 D  ; iterate through the list of items
  .S FLG="" F  D  Q:FLG  ; randomly select a target list
  ..S R=$R(5)+1  ; this will yield (0-4)+1 -> 1-5
  ..I $L($G(@ND@(R)),",")<4 D  S FLG=1  ; has the target list less then 5 items?
  ...S @ND@(R)=$G(@ND@(R))_$S($G(@ND@(R))="":"",1:",")_ITMS(I)  ; if so, add one
  ;
  F I=1:1:5 U 0 W "List "_I_": "_@ND@(I),!  ; print the target lists
  Q

Example using syntax highlighting