Rearrange array elements (random order)

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: Dorian (MJT support), JRL, Phil Pendlebury

Post Reply
hagchr
Automation Wizard
Posts: 331
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Rearrange array elements (random order)

Post by hagchr » Tue Aug 02, 2022 9:37 am

A simple algorithm for rearranging array elements in random order. The bottom part is just to convert the array back to a string to show it in an MDL>. Maybe ArrayRand> could be considered for future updates?

Code: Select all

// Script to rearrange array elements in a random order. 
//
Let>str=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25
Separate>str,COMMA,arr
//
// Rearrange array in random order
Let>k=arr_Count
While>k>0
  Let>tmp=arr_%k%
  Random>k,valRand
  Add>valRand,1
  Let>arr_%k%=arr_%valRand%
  Let>arr_%valRand%=tmp
  Sub>k,1
EndWhile
//
//
// Convert array to string for MDL>
Let>strRes=
Let>k=0
While>k<arr_Count
  Add>k,1
  Let>tmp=arr_%k%
  If>k<>arr_Count
    Let>strRes=%strRes%%tmp%%COMMA%
  Else
    Let>strRes=%strRes%%tmp%
  EndIf
EndWhile
//
Let>MSG_HEIGHT=250
Let>MSG_Width=500
MDL>Ori:   %str%%CRLF%Res: %strRes%

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts