01-05-2022, 11:34 AM
Python Random Functions
When creating projects with Experiment Builder, you can make use of many of the random functions that are included in the python random module detailed here https://docs.python.org/3/library/random.html.
Some common examples are illustrated below.
Data types
Experiment Builder recognizes various data types, which are important when they are inputs for a function - they are:
random.choice()
This function allows you to randomly select one item from a list of items, for example, to randomize the location of a stimulus on a display screen from a list of point values. The function uses the format =random.choice(list_value). In the example, the ‘List_Locations’ variable is a list of three point-values (location x,y values), and the variable ‘Simuli_Location’ is updated with a random choice of one of these values.
random.int()
This function allows you to randomly select an integer from a range (based on start and end values), for example, to randomly choose the duration of a timer trigger-based within a set range. In the example, a variable ‘Gap_Interval’ is updated with a random integer value between 500 and 1000 (note that in Python the end value of ranges is non-inclusive, so make it one more than the final desired value, e.g. 1001 so that 1000 can be randomly selected).
random.randrange()
This function allows you to randomly select an integer from within a range limited by a step size value, for example, a random value between 500 and 1000 (again the end value is non-inclusive) in steps of 50 (example values of 500, 550, 600 etc).
random.shuffle()
This function randomly shuffles the order of items in a list. To utilize this function in Experiment Builder requires a placeholder empty list variable (e.g. ‘dummy’ shown below) to occupy the Attribute cell in the Update Attribute action.
With a blank ‘dummy’ variable, you can shuffle the order of the list and then assign each value (index) of the list to your project. In the example below, the list of three point-values (location) are shuffled and the first item (index 0) is allocated to the Target_Location variable, the second (index 1) to the Distractor_Location variable and the third item (index 2) to the Competitor_Location variable. Please note that to assign a list item requires the Value cell to begin with an “=” and the index value is given within “[]”.
Final Comments
There are many more random functions within the Python Random module, many of which work within Experiment Builder. You can read more about them here https://docs.python.org/3/library/random.html.
When creating projects with Experiment Builder, you can make use of many of the random functions that are included in the python random module detailed here https://docs.python.org/3/library/random.html.
Some common examples are illustrated below.
Data types
Experiment Builder recognizes various data types, which are important when they are inputs for a function - they are:
- String - text, any value provided within “ “ marks and any keyboard key-value, e.g. Hello, “1”, and Esc
- Number - either integer (1,2,3) or float values (0.5, 5.23, 10.2)
- Boolean - True or False
- Color - RGB values (Red, Green, Blue) on a scale from 0-255, eg. 1,1,2 or 255,255,255
- Point - Location values given as x,y coordinates, e.g. 100,200 or 540,960
- List - A list, defined within [], can be formed of any of the above, eg. [(255,255,255), (0,0,0)] is a list of two colors (black and white); [(960, 540), (660, 540), (1260, 540)] is a list of three point values. *the first item in the list is indexed to 0, the second to 1 etc.
random.choice()
This function allows you to randomly select one item from a list of items, for example, to randomize the location of a stimulus on a display screen from a list of point values. The function uses the format =random.choice(list_value). In the example, the ‘List_Locations’ variable is a list of three point-values (location x,y values), and the variable ‘Simuli_Location’ is updated with a random choice of one of these values.
random.int()
This function allows you to randomly select an integer from a range (based on start and end values), for example, to randomly choose the duration of a timer trigger-based within a set range. In the example, a variable ‘Gap_Interval’ is updated with a random integer value between 500 and 1000 (note that in Python the end value of ranges is non-inclusive, so make it one more than the final desired value, e.g. 1001 so that 1000 can be randomly selected).
random.randrange()
This function allows you to randomly select an integer from within a range limited by a step size value, for example, a random value between 500 and 1000 (again the end value is non-inclusive) in steps of 50 (example values of 500, 550, 600 etc).
random.shuffle()
This function randomly shuffles the order of items in a list. To utilize this function in Experiment Builder requires a placeholder empty list variable (e.g. ‘dummy’ shown below) to occupy the Attribute cell in the Update Attribute action.
With a blank ‘dummy’ variable, you can shuffle the order of the list and then assign each value (index) of the list to your project. In the example below, the list of three point-values (location) are shuffled and the first item (index 0) is allocated to the Target_Location variable, the second (index 1) to the Distractor_Location variable and the third item (index 2) to the Competitor_Location variable. Please note that to assign a list item requires the Value cell to begin with an “=” and the index value is given within “[]”.
Final Comments
There are many more random functions within the Python Random module, many of which work within Experiment Builder. You can read more about them here https://docs.python.org/3/library/random.html.