community
cancel
Showing results for 
Search instead for 
Did you mean: 

Alteryx Designer Knowledge Base

Definitive answers from Designer experts.

Generating a List of Random Numbers with an Iterative Macro

Sr. Instructional Designer
Sr. Instructional Designer
Created on

Iterative macros can be used to repeat a process a desired number of times. Say, for instance, you want to generate a list of 10 random numbers.  Rather than create ten different formulas for the values, wouldn’t it be great to simply tell Alteryx how many values to create, hit “Run”, and voila? Well, with a few clicks to create a simple iterative macro and some knowledge of engine constants, you can! With the particular examples provided in the sample workflow, I’ve imagined two tasks. One is to generate 10 random numbers between 0 and 100. This input data only requires the number of values to output (Figure 1, A). The second task is to create a number of values associated with a group.  In this case, the input data has two fields: the Group Name and the number of values to create for that group (Figure 1, B).  The input data feeds directly into the iterative macro which begins the process of repeatedly generating random values the specified number of times.

 

Figure 1: Input data for the two sample scenarios

Figure1.JPG

 

The number of times the macro repeats the process of generating a random value is controlled by two things: the user-defined value of repetitions in the input data and the engine iteration number within the macro. Once a workflow is defined as an iterative macro, certain constants are then made available for use within the workflow (Figure 2).  For example the engine iteration number, a constant which records the number of times a macro has run, is a usable field in the Formula tool (Figures 3). As a usable numeric field, I can insert the engine iteration number in a formula to record the number of repetitions needed to generate the values I need. Because the number of Engine iterations starts at “0”, adding “1” to this value scales it appropriately to the value(s) entered in the input data.

 

Figure 2: Iterative macros activate engine constants, and specifically the iteration number, for use within workflows. Note that iteration numbers begin at 0

Figure2.JPG

 

Figure 3: Engine constants can be used to build expressions in the formula tool

Figure3.JPG

 

 

This engine iteration number helps control the number of times a repetitive process runs by serving as a logical comparison in a filter tool: does the number of values I set out to create equal the number of times the macro has run? In other words, does my input data value equal my engine iteration number +1?  If the answer to that question is “no”, then the workflow will run again after storing the data it just created using a union tool.  If that logic’s answer is “yes”, then the process has generated the desired number of outcomes and leaves the iterative macro, along with any previously generated data that has been unioned from previous iterations (Figure 4). 

 

Figure 4: If the number of desired values has not yet been created (per the logic of the filter), then the process of generating values will repeat.  Regardless, the random values that are created are stored with a union tool and output once the Filter's logic is "True"

Figure4.JPG

 

After leaving the macro, the data can be summarized to visualize grouped data, selected for certain fields, or even fed through Data Investigation tools for further analyses (see attached v10.6 workflow). 

Attachments