Unexpected Random behaviour in Recursive Function.

Hi, I'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'm using then is a recursive rule so that I can pass values forward to check what the acceptable values are.

However I'm having a strange behaviour which I believe is the fault of the random number generation in Appian. The first recursion will perform exactly as expected however every successive call will just select the item that is at the front of the list.

Can anyone explain this issue?

 

The code looks like this and it rather betrays the less than professional use case for this rule.

/*Blakej3_Minesweeper_recursivelyFindMine_B*/
load(
  /*Find index of the array to target.*/
  local!index:roundup(
      rand() * (length(ri!boardIds)),
      0
    ),
  if(
    contains(
      ri!mines,
      ri!boardIds[local!index]
    ),
    /*If the index has already been added to mines try again.*/
    rule!Blakej3_Minesweeper_recursivelyFindMine_B(
      boardIds: ri!boardIds,
      mines: ri!mines
    ),
    /*Otherwise update the values and restart.*/
    {
      mines:  append(ri!mines, ri!boardIds[local!index]),
      boardIds: remove( ri!boardIds, local!index )
    }
  )
)

The rule to generate each mine is also recursive, this is both a legacy and a helpful way of removing values incase the list is prepopulated, it looks like this.

/*Blakej3_Minesweeper_recursivelyFindMines*/
if(
  /*Abort recursion at the end or if there are no more values.*/
  and(
    ri!calls >= 1,
    not(rule!APN_isEmpty(ri!boardIds))
  ),
  load(
    /*Generate the next mine*/
    local!nextMine: rule!Blakej3_Minesweeper_recursivelyFindMine_B(
      boardIds: ri!boardIds,
      mines: ri!mines
    ),
    /*Recurse*/
    rule!Blakej3_Minesweeper_recursivelyFindMines(
      mines: local!nextMine.mines,
      boardIds: local!nextMine.boardIds,
      calls: ri!calls-1
    )
  ),
  /*At the end of the chain return the values.*/
  {
    mines: ri!mines,
    boardIds: ri!boardIds
  }
)

Both mines and boardIds are dictionaries in the form of {x:_, y:_}

  Discussion posts and replies are publicly visible