<?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>Unexpected Random behaviour in Recursive Function.</title><link>https://community.appian.com/discussions/f/rules/12365/unexpected-random-behaviour-in-recursive-function</link><description>Hi, I&amp;#39;m working on a rule that selects random values from a list. This rule must pick a given number of values and it may not pick the same one twice. 
 Logically what I&amp;#39;m using then is a recursive rule so that I can pass values forward to check what</description><dc:language>en-US</dc:language><generator>Telligent Community 12</generator><item><title>RE: Unexpected Random behaviour in Recursive Function.</title><link>https://community.appian.com/thread/64307?ContentTypeID=1</link><pubDate>Tue, 29 Jan 2019 12:55:37 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:d8ab9394-4aa5-49dc-ae3d-97707aca4e18</guid><dc:creator>jonathanb0001</dc:creator><description>&lt;p&gt;Wow been a while since this thread was active. Thanks for the response that&amp;#39;s a really interesting approach that I&amp;#39;d never considered.&lt;/p&gt;
&lt;p&gt;I&amp;#39;m a little concerned about using an expression like that due to the nature of the random function being applied multiple times.&lt;br /&gt; Indexing a single value against a list guarentees that a single value will be selected however I don&amp;#39;t see how your rule can ensure that one and only one value will be selected for each time the rule runs over the list.&lt;br /&gt; That just sets off all sorts of alarm bells in my head.&lt;br /&gt; &lt;br /&gt; Didn&amp;#39;t know the Navy was looking for me. Jokes on them I don&amp;#39;t own a boat so they&amp;#39;ll be searching for a long time :P&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Unexpected Random behaviour in Recursive Function.</title><link>https://community.appian.com/thread/64305?ContentTypeID=1</link><pubDate>Tue, 29 Jan 2019 11:39:06 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:128c3ce3-232e-4f40-a874-d6adb3616ea8</guid><dc:creator>chrisc651</dc:creator><description>&lt;p&gt;You could also use reduce which is recursive, to remove a number of entries from the whole list, and then use difference to get the ones which have been removed.&amp;nbsp; In this case the array of integers passed to reduce is only used to control the number of times it is called. Have the Navy given you a call yet ;)&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;difference(
  ri!inputArray,
  reduce(
    rule!removeRandArrayEntry,
    ri!inputArray,
    enumerate(
      ri!numTimes
    )
  )
)&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Unexpected Random behaviour in Recursive Function.</title><link>https://community.appian.com/thread/54937?ContentTypeID=1</link><pubDate>Tue, 08 May 2018 15:53:48 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:3a724900-40d8-4afb-afbc-7f0e2c793e83</guid><dc:creator>jonathanb0001</dc:creator><description>It&amp;#39;s now working however I can&amp;#39;t find a satisfying reason why.&lt;br /&gt;
I added a length parameter which tracked the range for the random number and it was constantly 1.&lt;br /&gt;
I expanded the lists of dictionaries for boardIds by adding a forEach statement which just returned each item. Now it works... My best guess is that Appian was struggling with list/dictionaries (Maybe it was seeing a list of list of dictionaries? However in that case I&amp;#39;d have expected it to append all of them at once.). And that the for each restructured it into something it found easier to handle.&lt;br /&gt;
Yeah, It doesn&amp;#39;t really make sense to me.&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Unexpected Random behaviour in Recursive Function.</title><link>https://community.appian.com/thread/54806?ContentTypeID=1</link><pubDate>Fri, 04 May 2018 16:58:51 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:2add87fb-902c-4025-80cb-fc401f98ecc5</guid><dc:creator>Josh</dc:creator><description>&lt;p&gt;I think the root of this issue lies in your implementation somewhere. I whipped up a simple recursive rule that uses rand() and returns ri!numTimes random numbers from 1 - ri!range. Running this rule results in a list of different random numbers each time. This seems to rule out any issues with rand() and recursion.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;if(
  ri!numTimes = 0,
  null,
  append(
    roundup(rand() * ri!range),
    rule!this_rule(
      numTimes: ri!numTimes - 1,
      range: ri!range
    )
  )
)&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Unexpected Random behaviour in Recursive Function.</title><link>https://community.appian.com/thread/54800?ContentTypeID=1</link><pubDate>Fri, 04 May 2018 15:07:05 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:cc0a18b8-8d26-40d9-a13d-84d14ee5caa2</guid><dc:creator>jonathanb0001</dc:creator><description>Afraid not. That doesn&amp;#39;t explain the problem with the behaviour as if it wasn&amp;#39;t being reevaluated it would just take the same index value each time rather than taking a random one and then always taking the 1st.&lt;br /&gt;
&lt;br /&gt;
However to be thorough I did try this. Even after both loads are changed to with it doesn&amp;#39;t change how the process works.&lt;br /&gt;
&lt;br /&gt;
I think the problem with your solution is that despite appearances each local!index is created in its own separate Load() and as theres no user interaction it actually never receives a signal telling it to refresh the value anyway.&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Unexpected Random behaviour in Recursive Function.</title><link>https://community.appian.com/thread/54798?ContentTypeID=1</link><pubDate>Fri, 04 May 2018 15:00:42 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:1ef3ed59-e057-4055-8eed-7a7725a2f8e7</guid><dc:creator>Carlos Santander</dc:creator><description>Your local!index variable doesn&amp;#39;t get updated because it&amp;#39;s in a load(). Change that to with() and it should work.&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>