| Reference | Downloads | Github

Randomize the number of repeats($nReps)


I want to randomize the number of repeats($nReps) in ‘trials’ loop

I have to create an experiment in which we make 12 blocks of random trials.

In each trials, when ‘space’ pressed, the image feedback appears and
when the trials are repeated for the number between 5 and 16 times,
the chocolate feedback appears.

Each block contains randomized number of trials(5-16 times).

What did you try to make it work?:
I used code component.

import numpy
nrepeats = numpy.random.randint(5,16)

What specifically went wrong when you tried that?:
when I put the code at ‘Begin Experiment’
it worked, but at ‘Begin Routine’, it didn’t work with no error output.

My problem is that If I put it at ‘Begin Experiment’,
the number of repeats is same for each of 12 blocks.

Thank you for any help or idea!


Code that runs at the beginning of a routine is inside the loop, so it is too late to affect the creation of the loop, which has already occurred. So what you should do is:

  • run that code at the start of the experiment (as you have done above), so that a value for the variable will be available for the very first run of the loop.
  • in the “begin routine” tab, you’ll probably want to save the chosen value in the data, e.g:
thisExp.addData('repeats', repeats)

On the last iteration of the trials loop, you’ll need to update the value of nrepeats for the next time it runs. So put something like this in the “end routine” tab:

if trials.nRemaining == 0:
    nrepeats = numpy.random.randint(5,16)

NB the second parameter in the call to randint is exclusive, so it will return values ranging from 5 to 15.

Hi, Michael.
Thank you for your answering!

I have another questions. Actually I changed my code referring to the link below. So I could give a visual feedback when the participants gives a response.
Give visual feedback based on response without ending trial - #4 by dvbridges

Then, I combined the visual feedback code with the feedback code of Balloon Analogue Risk Task(BART) DEMO file. The number of button press for feedback was randomized by Excel functions.

I could give not only the immediate feedback for responses, but also the “reward feedback” which is given for several button presses.

But, there are two problems. First, I would like to put intertrial interval for about 20 seconds after “reward feedback”, but in my routine, the [space] key response also works intactly in intertrial interval which is time for pause.
Second, I would like to put black background, using polygon rectangle, but, it’s not possible to make the polygon appear behind the other stimulus.

Thank you again for your help!


Sorry, I forgot to upload my fileinstrumentalDEMO7.psyexp (20.1 KB)

The drawing order of components is from top (first) to bottom (last). So if you want the black rectangle stimulus to appear in the background, move it to the top (by right-clicking on its icon).

I haven’t looked at your file, and am not entirely sure I understand the issue, but don’t forget that you can split your trial into multiple consecutive routines. e.g. allow the space key to end the first routine, and then immediately present a second routine for the inter-trial interval. This second routine would be set to have a fixed duration of 20 seconds and to not respond to key presses.