| Reference | Downloads | Github

How to set different duration time as the same in number

Hi, everyone. Forgive me will ask a silly question. It’s my first time using psychopy, everything goes well, but I want make my experiment more easily to analyze. Then I met a question difficult for me to solve, I really need your help.
I designed my experiment with builder. During my experiment, I will show participants a cross for different duration time as 2s, 4s, 6s and 8s, so I write two lines code with code component as:

duratimelist = [2, 4, 6, 8]
duratime = duratimelist[randint(0,4)]

and this code goes well. My problem is I don’t want to show the cross in really random duration time, actually I want to show the cross in 2s, 4s, 6s and 8s 12 times each. I think I need a code to set this condition but I don’t know exactly what kind of code. Pleas help me. Really thank you very much.

This is very common. What you want to do is create all the combinations you want, randomly shuffle that list, and then extract items from the list as required.


# probably in the "Begin experiment" tab of a code component,
# create a list with 48 elements:
duration_list = [2, 4, 6, 8] * 12 

#randomise its order: 

Then at later points in the experiment, you can extract the next value in the list. e.g. perhaps in the “Begin routine” tab, get a value for the current trial:

trial_duration = duration_list.pop()

NB you probably want to save that value in the data so at the analysis stage you know what happened, so also add a line like this:

# ensure it gets saved in the data for this trial:
thisExp.addData('duration', trial_duration)

Hi, Michael. Thank you very much.
It worked! Really helped me a lot. This “problem” confused me for a while.
I think I should learn some thing about python or more about psychopy.
Really thank you very much.


Hi, Michael.
Sorry to bother you again.
As I replied earlier, the code you taught me goes really well, and the trial_duration form was appeared in the data file. But, only the title appeared, the duration time didn’t appeared in the csv file or the log file. Would you help me again? I am not sure wether I should ask this question in a new topic. I know that directly ask you a question again is not good, but forgive and please help me.
Thank you very much.


That is strange. Try adding a line like this:

# ensure it gets saved in the data for this trial:
thisExp.addData('duration', trial_duration)
print(trial_duration) # just temporarily, for debugging

and check the output window after the experiment has run what values are being printed.

Also NB the code component needs to be above any component that uses the $trial_duration variable, so that it gets the correct value of the variable as updated for this trial, and not the previous one.

Hi, Michael.
Thank you for your reply and your kindness.
I did put the code component above the component that would run the trial_duration variable.
I tried to print the duration time out, as the picture below shows, it worked well I think.

But it still didn’t appear in the csv file and the log file.

And there is another not good news, the key responses also gone from the csv file, but the good news is key responses all recorded in the log file.

I still use the previous version, wether updating to the latest one could solve the problem?

Your replies really helped me, and made me less nervous.
Thank you so so much.


Can you send a screenshot of the “data” tab of your “experiment settings” dialog?

Hi, Michael.

This is the screenshot of the data tab of experiment settings.

If I made some basic mistakes, please forgive me taking so much of your time.


Please uncheck the “Save csv file (summaries)” check mark. This format isn’t very useful to anyone these days (it tries to do some summaries of the data (taking means and so on), but what you want are the raw, trial-by-trial lines). I’m hoping that will show you more clearly which .csv files to examine.

Also uncheck the Excel option. That can cause performance problems.

We really should hide those two options safely away from people.

I unchecked the two marks you told me, but it still not work…


It’s not the lack of the duration field that is puzzling so much as the lack of almost all other information as well. We probably need to see your Builder.psyexp file uploaded here.

Hi, Michael.
Really thanks to help me a lot.
I uploaded my builder file named “test”.
Hope that will help you to find the problem I met.
Really thanks for your kindness.


test.psyexp (20.3 KB)

In your loop dialog, please check “is trials”. This tells Builder that this loop is running trials and data should be saved.

The option is there because sometimes we just want to repeat things within a trial and not have any data associated with it. But almost always, you want this enabled.


What a stupid mistake I made.
Sorry bother you with such a stupid problem. I didn’t note it. Really sorry!
But thank you for solving this problem for me.
Please don’t ignoring my future questions though they probably be other stupid questions.
Really thank you for your kindness.
Have a good day and good luck.


Don’t worry, it’s a subtle issue. Good luck with your study.