Now Shipping! – The EyeLink 3; combined head and eye tracking at up to 1000 Hz.

FAQ: How can I use common Python random functions?
#1
You can use many of the functions from Python's random module directly within Experiment Builder to add randomization to your projects. This guide covers the most common functions.

Understanding Data Types
Before using these functions, it's important to know how Experiment Builder handles different data types:
  • String: Text, enclosed in " marks. Examples: "Hello", "1", "Esc".
  • Number: Can be an integer (e.g., 1, 100) or a float (e.g., 0.5, 5.23).
  • Boolean: True or False.
  • Color: A tuple of three RGB values from 0-255. Example: (255, 0, 0) for red.
  • Point: A tuple of X,Y coordinates. Example: (960, 540).
  • List: An ordered collection of items, enclosed in []. A list can contain any data type.
    • Example: [(960, 540), (660, 540), (1260, 540)] is a list of three Points.
    • Note: Lists are 0-indexed, meaning the first item is at index [0], the second is at [1], and so on.
Common Random Functions
  • random.choice(list)
    Select one random item from a list. This is useful for randomizing stimulus location, color, or content.
    For example, if you wanted to pick a random value from a predefined list:
    Code:
    =random.choice(@List_Locations.value@)
       
  • random.shuffle(list)
    Randomly shuffle the order of items in a list in-place. This function modifies the original list and returns None, which requires a special setup in the Update Attribute node.
    For example, if you wanted to randomize the order of a list of location values in place:
    Code:
    =random.shuffle(@List_Locations.value@)
       
    Note: Because random.shuffle() returns nothing, you must call it in an Update Attribute node where the Attribute is set to a placeholder (or "dummy") variable. The Value should be the function call. This executes the shuffle without causing an error.
       
  • random.randint(A, B)
    Select a random integer between A and B, inclusive (meaning both a and b can be chosen). This is useful for randomizing durations.
    For example, if you wanted to select any possible integer between 500 and 1001:
    Code:
    =random.randint(500, 1001)
       
  • random.randrange(A, B, step)
    Select a random integer from A range to B with a defined step size between values. The B value is exclusive.
    For example, if you wanted to select a value between 500 and 1000 in increments of 50:
    Code:
    =random.randrange(500, 1001, 50)
       


Further Reading
Many other functions are available in the Python random module. For the full list, see the official Python documentation.