PyCod1297
PyCod1297

Reputation: 37

Is there a way to randomize the order of sub loops in a main loop?

I have 6 different blocks (for-loops) that are part of a main for-loop. Each block, consisting of 6 randomly picked stimuli, is supposed to be presented twelve times. The presentation of the blocks should happen in a random order.

So far, I created a main loop around the six blocks that repeats twelve times and I planned to randomize the order of the blocks with PsychoPy's TrialHandler. However, this does not work the way it should and I have to figure out another way. I was thinking:

  1. I could make a list (of numbers or strings) and shuffle this list and then create if-conditions for the sub loops i.e. "if "Block1" is list[0], then the for-loop Block1 is initialized. Does something like that work? If it does, how do I implement it because, ideally, I want to generate the order of the sub loops with the list.

  2. The other idea concerns the TrialHandler and is thus specific to PsychoPy. I was wondering if it is possible to create an excel file containing the different blocks and add it to the conditions parameter of the main loop. If every block would be a row, PsychoPy would be able to randomize these. However, I am unsure if this works and how the blocks could be added to an excel file.

Does any of these solutions seem reasonable or is there another way to achieve a randomization?

Upvotes: 1

Views: 150

Answers (1)

jcf
jcf

Reputation: 602

As Python functions are essentially objects, you can assign them to lists or dictionaries:

from random import randint


def foo():
    # do things
    print('foo')

def bar():
    print('bar')

funcs = [foo, bar]

random_int = randint(0, len(funcs)-1) # generate a int between 0 and last entry of the list
funcs[randint]() # this calls the function in position randon_int

Upvotes: 1

Related Questions