<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.appian.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Shuffling an array randomly</title><link>https://community.appian.com/discussions/f/rules/20102/shuffling-an-array-randomly</link><description>I have an array of CDTs say questions with question structured as: 
 { 
 id: 1, 
 statement: &amp;quot;Text string of question&amp;quot;, 
 options: {List of Options} 
 } 
 Like multiple values like the above. 
 
 I would like to shuffle this array of questions randomly</description><dc:language>en-US</dc:language><generator>Telligent Community 12</generator><item><title>RE: Shuffling an array randomly</title><link>https://community.appian.com/thread/78582?ContentTypeID=1</link><pubDate>Fri, 18 Dec 2020 15:30:40 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:1c700c7e-0a69-4f96-8f9d-d5a680364845</guid><dc:creator>Mike Schmitt</dc:creator><description>&lt;p&gt;Something like that would need to be built into the a!queryEntity function or one of its components - you could always file a feature request ticket with Appian Support, however I wouldn&amp;#39;t expect it to be prioritized anytime soon without a strong use case on your end.&lt;/p&gt;
&lt;p&gt;Out of boredom I went ahead and implemented a standalone function where you input the desired number of items and it returns a randomized list of integer indices 1...N, where we&amp;#39;re guaranteed to have no duplicates or missing numbers.&amp;nbsp; This integer array could then be used to index into any particular array in a randomized manner.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="java"&gt;a!localVariables(
  
  local!targetSize: 25,
  
  local!dict: a!forEach(
    enumerate(local!targetSize),
    
    a!map(
      integer: fv!index,
      sort: rand()
    )
  ),
  
  local!sorted: todatasubset(
    local!dict,
    a!pagingInfo(
      startIndex: 1, batchSize: -1,
      sort: a!sortInfo(field: &amp;quot;sort&amp;quot;, ascending: true())
    )
  ),
  
  local!sorted.data.integer
)&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Shuffling an array randomly</title><link>https://community.appian.com/thread/78575?ContentTypeID=1</link><pubDate>Fri, 18 Dec 2020 06:54:29 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:e5aa97b9-beb5-4460-99c7-2abdc88643cc</guid><dc:creator>adityaamba</dc:creator><description>&lt;p&gt;Thanks Mike,&lt;/p&gt;
&lt;p&gt;This was a really innovative way to sort and shuffle a large array at low perofromance cost. But I feel that since we have the functionality of fetching randomly sorted records direct from SQL query in MySQL, Appian should incorporate a way to use that functionality as it will save a great coding time and performance load will be reduced.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Shuffling an array randomly</title><link>https://community.appian.com/thread/78554?ContentTypeID=1</link><pubDate>Thu, 17 Dec 2020 14:42:15 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:880bfb19-b14f-43b6-b6f5-cba6093a6bea</guid><dc:creator>Mike Schmitt</dc:creator><description>&lt;p&gt;I like it - though it should be noted that this could occasionally (probably very rarely) return only 4 results if none of the 50 &amp;quot;coin flips&amp;quot; come up for one of the 5 numbers.&amp;nbsp; I assume if you push the &amp;quot;random array length&amp;quot; value high enough you&amp;#39;ll eventually get zero failures, though i&amp;#39;m not sure what that&amp;#39;ll do to the efficiency of the rule.&lt;/p&gt;
&lt;p&gt;I just tested and this happened 3 out of 10,000 times (though on some other runs of the same code it happened 0 times):&lt;br /&gt;&lt;img src="/resized-image/__size/320x240/__key/communityserver-discussions-components-files/15/pastedimage1608215896597v1.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="java"&gt;a!flatten(
  a!forEach(
    enumerate(10000), /* number of times to test */
  
    a!localVariables(
      local!length: 5,
      local!array: floor(rand(local!length*10) * local!length) + 1,
      if(
        length(union(local!array, local!array)) &amp;lt; local!length,
        &amp;quot;flag&amp;quot;,
        {}
      )
    )
  )
)&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Shuffling an array randomly</title><link>https://community.appian.com/thread/78552?ContentTypeID=1</link><pubDate>Thu, 17 Dec 2020 14:12:15 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:0fe377c2-3710-4473-8833-e3ee90e948c7</guid><dc:creator>normanc</dc:creator><description>&lt;p&gt;&lt;span&gt;This will give you the numbers 1..5 in a random order, which could be used as an index for the CDT array.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="text"&gt;a!localVariables(
  local!array: floor(rand(50)*5)+1,
  union(local!array, local!array)
)&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Shuffling an array randomly</title><link>https://community.appian.com/thread/78551?ContentTypeID=1</link><pubDate>Thu, 17 Dec 2020 14:10:17 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:466d9091-ab8a-4ec5-a6b8-7a874683f79a</guid><dc:creator>Mike Schmitt</dc:creator><description>&lt;p&gt;There are various ways to do this, but anything you do you&amp;#39;ll have to build yourself.&lt;/p&gt;
&lt;p&gt;A quick-and-dirty way I just thought of:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;use a!forEach to loop over the CDT array and use updateDictionary() to add a new ad-hoc property to each one, i.e. &amp;quot;order&amp;quot;, set to the value of a rand() call.&amp;nbsp; This will give a random decimal between 0 and 1 to all items; they should almost always be unique as far as I know.&lt;/li&gt;
&lt;li&gt;Sort the array based on the &amp;quot;order&amp;quot; decimal (use the &amp;quot;toDataSubseet&amp;quot; trick to sort an array of dictionary based on the value of a property).&lt;/li&gt;
&lt;/ol&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>