proteus
proteus

Reputation: 505

Randomize list in Azure Logic Apps

Is it possible to randomize the elements of an array using just functions from WDL in Logic apps?

Upvotes: 0

Views: 1061

Answers (2)

Adam Marczak
Adam Marczak

Reputation: 2351

Let's imagine you have array from 1 to 10

enter image description here

You create one lement randomized array variable

first(variables('Array'))

enter image description here

And a temp value (tempValue) and temp array (temp) (used in later steps because self referencing is not allowed)

enter image description here

Then you create until loop like so

enter image description here

With Until condition being

equals(length(variables('Randomized')), length(variables('Array')))

Calculate random index tempValue in array (random index at which we will split array)

rand(0,sub(length('Randomized'),1))

And combine new array temp by inserting current iteration value at random place

union(
  take(
    variables('Randomized'),
    variables('tempInteger')
  ),
  array(variables('Array')[add(iterationIndexes('Until'),1)]),
  skip(
    variables('Randomized'),
    variables('tempInteger')
  )
)

And after condition statement set randomized variable to temp value

Which after the last iteration will net you randomized input table

enter image description here

And full code example

{
  "definition": {
    "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
    "actions": {
      "Init_Randomized_One_Element_Array": {
        "inputs": {
          "variables": [
            {
              "name": "Randomized",
              "type": "Array",
              "value": [
                "@first(variables('Array'))"
              ]
            }
          ]
        },
        "runAfter": {
          "Init_Unrandomized_Array": [
            "Succeeded"
          ]
        },
        "type": "InitializeVariable"
      },
      "Init_Randomized_Temp_Array": {
        "inputs": {
          "variables": [
            {
              "name": "temp",
              "type": "Array"
            }
          ]
        },
        "runAfter": {
          "Init_Randomized_One_Element_Array": [
            "Succeeded"
          ]
        },
        "type": "InitializeVariable"
      },
      "Init_Temp_Calculated_Value": {
        "inputs": {
          "variables": [
            {
              "name": "tempInteger",
              "type": "integer"
            }
          ]
        },
        "runAfter": {
          "Init_Randomized_Temp_Array": [
            "Succeeded"
          ]
        },
        "type": "InitializeVariable"
      },
      "Init_Unrandomized_Array": {
        "inputs": {
          "variables": [
            {
              "name": "Array",
              "type": "Array",
              "value": [
                1,
                2,
                3,
                4,
                5,
                6,
                7,
                8,
                9,
                10
              ]
            }
          ]
        },
        "runAfter": {},
        "type": "InitializeVariable"
      },
      "Until": {
        "actions": {
          "Add_random_element_to_temp_array": {
            "inputs": {
              "name": "temp",
              "value": "@union(\r\n\ttake(\r\n\t\t   variables('Randomized'),\r\n\t\t   variables('tempInteger')\r\n\t),\r\n\tarray(variables('Array')[add(iterationIndexes('Until'),1)]),\r\n\tskip(\r\n\t   variables('Randomized'),\r\n\t   variables('tempInteger')\r\n\t)\r\n)\r\n"
            },
            "runAfter": {
              "Random_Index_to_insert": [
                "Succeeded"
              ]
            },
            "type": "SetVariable"
          },
          "Random_Index_to_insert": {
            "inputs": {
              "name": "tempInteger",
              "value": "@rand(0,sub(length('Randomized'),1))"
            },
            "runAfter": {},
            "type": "SetVariable"
          },
          "Set_randomized_from_temp": {
            "inputs": {
              "name": "Randomized",
              "value": "@variables('temp')"
            },
            "runAfter": {
              "Add_random_element_to_temp_array": [
                "Succeeded"
              ]
            },
            "type": "SetVariable"
          }
        },
        "expression": "@equals(length(variables('Randomized')), length(variables('Array')))",
        "limit": {
          "count": 60,
          "timeout": "PT1H"
        },
        "runAfter": {
          "Init_Temp_Calculated_Value": [
            "Succeeded"
          ]
        },
        "type": "Until"
      }
    },
    "contentVersion": "1.0.0.0",
    "outputs": {},
    "parameters": {},
    "triggers": {
      "manual": {
        "inputs": {
          "schema": {}
        },
        "kind": "Http",
        "type": "Request"
      }
    }
  }
}

Upvotes: 2

DTRT
DTRT

Reputation: 11040

There is no direct way to randomize anything in a LogicApp.

I suppose you could come up with some pattern that could pseudorandomize a list entirely within a Logic App, but....

The more 'correct' way to handle this would be with a Function.

Upvotes: 0

Related Questions