| Reference | Downloads | Github

Choosing multiple items randomly from a single variable

Windows 10
Psychopy v3.0.0b9

I am completely new to Psychopy and wanted to build an experiment which presents 9 strings (consonants) respectively for a number of times (100 iterations). The 9 consonants are randomly chosen from the list of all of the consonants in each round. But I don’t know how to extract just 9 items from the whole list. If I need to write code, I would appreciate it if you describe the details.

Thanks in advance.

P.S. I know the python code would be something like this (for clarifications only):

consonants = [list_of_consonants]
target_list_for_a_loop = []
for i in range(9):

That would probably work fine, but could be little more concise, as you can extract a list of desired size directly from the numpy.random.choice() function (i.e. we will import choice() from the numpy library rather than the Python standard library, precisely because that random library is more flexible):

Begin experiment tab:

from numpy.random import choice
consonants = [list_of_consonants]

Begin routine tab:

if your_loop_name.thisN == 0:
    target_list_for_a_loop = list(choice(consonants, size = 9))

I think you want the list of consonants to be constant across all the iterations of the loop, hence we insert a check to only draw a new sample on the first iteration of the loop.

We also make it a list explicitly, as numpy functions tend to return arrays, and lists are probably easier for you to work with.

1 Like