{"id":823,"date":"2009-10-28T09:00:41","date_gmt":"2009-10-28T09:00:41","guid":{"rendered":"http:\/\/www.mjtnet.com\/blog\/?p=823"},"modified":"2009-10-29T10:22:29","modified_gmt":"2009-10-29T10:22:29","slug":"randomly-shuffle-an-array","status":"publish","type":"post","link":"https:\/\/www.mjtnet.com\/blog\/2009\/10\/28\/randomly-shuffle-an-array\/","title":{"rendered":"Randomly Shuffle an Array"},"content":{"rendered":"<p>I was recently asked how to get a list of files from a folder, and then randomly shuffle that list.  The following code retrieves the list of files to an array using GetFileList and Separate and then loops through the array shuffling it randomly:<\/p>\n<pre name=\"code\" class=\"macroscript\">GetFileList>c:\\docs\\*.*,file_list\r\nSeparate>file_list,;,files_array\r\nIf>files_array_count>0\r\n  \/\/randomize the array\r\n  Let>k=0\r\n  Repeat>k\r\n    Let>k=k+1\r\n    Random>{%files_array_count%-1},random_index\r\n    Let>random_index=random_index+1\r\n    If>random_index<>k\r\n        Let>Temp=files_array_%k%\r\n        Let>files_array_%k%=files_array_%random_index%\r\n        Let>files_array_%random_index%=Temp\r\n    Endif\r\n  Until>k=files_array_count\r\nEndif<\/pre>\n<p>Edit: As pointed out in the comments this is somewhat naive and could cause an element to be swapped back to its original position.  A better method is given in the comments.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was recently asked how to get a list of files from a folder, and then randomly shuffle that list. The following code retrieves the list of files to an array using GetFileList and Separate and then loops through the array shuffling it randomly: GetFileList>c:\\docs\\*.*,file_list Separate>file_list,;,files_array If>files_array_count>0 \/\/randomize the array Let>k=0 Repeat>k Let>k=k+1 Random>{%files_array_count%-1},random_index Let>random_index=random_index+1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts\/823"}],"collection":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/comments?post=823"}],"version-history":[{"count":8,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts\/823\/revisions"}],"predecessor-version":[{"id":843,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts\/823\/revisions\/843"}],"wp:attachment":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/media?parent=823"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/categories?post=823"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/tags?post=823"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}