Combinations

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Combinations

Postby fightcancer » Mon Mar 20, 2017 5:41 am

If I have X different items, how can I get MS to select a combo at random? The number of possible combos should be X! where X is the number of elements to choose from.

For example, If I have 3 items--A, B, C--and order doesn't matter, how can I get MS to generate a combo at random?

ABC
ACB
BAC
BCA
CAB
CBA

Thanks!
Why is the richest nation ever 2nd to last in childhood poverty (among developed nations), 49th in literacy, 37th in general health, 41st in infant mortality?
fightcancer
Macro Veteran
 
Posts: 155
Joined: Fri Apr 15, 2005 8:32 am

Re: Combinations

Postby Marcus Tettmar » Mon Mar 20, 2017 9:10 am

How about this:

Code: Select all
//Set up an array of items
Let>NumItems=3
ArrayDim>Chars,NumItems
Let>Chars_1=A
Let>Chars_2=B
Let>Chars_3=C

//loop to create random string of items
Let>theLen=0
Let>NewString={""}
While>theLen<NumItems
  Random>NumItems,RandID
  Let>RandID=RandID+1
  Let>ThisChar=Chars_%RandID%
  Pos>ThisChar,NewString,1,pGotAlready
  If>pGotAlready=0
    Let>NewString=%NewString%%ThisChar%
  Endif
  Length>NewString,theLen
EndWhile

//display result
MessageModal>NewString
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
User avatar
Marcus Tettmar
Site Admin
 
Posts: 6789
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK

Re: Combinations

Postby fightcancer » Mon Mar 20, 2017 2:56 pm

WOW!! That was impressive!

Thanks SO VERY much!!
Why is the richest nation ever 2nd to last in childhood poverty (among developed nations), 49th in literacy, 37th in general health, 41st in infant mortality?
fightcancer
Macro Veteran
 
Posts: 155
Joined: Fri Apr 15, 2005 8:32 am


Return to Technical / Scripting

Who is online

Users browsing this forum: No registered users and 11 guests