How to code to get a random time for the fixation cross?

Dear friends,

As I am new to psychopy, I would be grateful if someone can help me.
How should be the coding to gain a random number of time (e. g., 500 ms) for the fixation cross from Psychopy?

Thanks in advance.

Are you using Builder? If so, you could do the following:

  1. Include a code component in your trial routine (on the right side, click Custom/Code).
  2. In the “Begin Experiment” tab, define the minimum and maximum duration of your fixation cross in seconds. For example, use:
# Fixation cross duration
fixDurMin = 0.3 # minimum: 300 ms
fixDurMax = 0.7 # maximum: 700 ms
  1. In the “Begin Routine” tab, use:
# Fixation cross duration for this trial
fixDur = np.random.uniform(fixDurMin, fixDurMax) # random between minimum and maximum
thisExp.addData('fixation_duration',fixDur) # document the duration in your logfile
  1. In the text component for your fixation cross, which needs to be below the code component, set the “Stop” section to “duration (s)” and use:

Now PsychoPy will use your randomly generated variable instead of a fixed time.

  1. Optional: Set “Expected duration (s)” to the average of your minimum and maximum duration, i.e., 0.5 in the example case, to see the estimated (!) fixation cross duration in your routine

Thank you so much for your great help.

I followed your instruction but I do not know why the program does not run.
I also changed the place of the code component before and after the fixation cross component ( which is the text component), but it still does not run.
I really do not know how to fix it.

I would be thankful if you could help me.

I’m sorry, I made a little mistake in step 3 and corrected it in the first post. Please exchange the old “Begin Routine” code with the new code and try again.
The text component needs to be below the code component because it needs its information.

If it still doesn’t run, please describe what happens in more detail and copy the output from the Experiment Runner here because it might contain helpful error messages.

let me try it

Unfortunately, it still does not work.

I would be thankful if you could help me.

Do you see the code component called “code” in your routine? Right click that and move it up, to be above the textFixationCross component.

I did it. But i do not know why it still does not run.
it shows the same message.

I am really confused. i would be thankful if you could help me.

Can you share a picture of the current organization of your routine?

This is the routine I have made. I do not know what is wrong with it. I also moved the ‘‘code’’ component up.

Your routine looks correct to me now, and I think I found the cause of the problem. Because of the way PsychoPy imports the random functions from numpy, it was necessary to use np.random.uniform(fixDurMin, fixDurMax). Please add the np. as well. I’ve added it in the first post for other people to find immediately.

Here’s a working minimum example: random_fix_cross_duration.psyexp (8.5 KB)

Please note that I set the stimulus component to begin right after the random fixation cross by setting its start to “condition” and fix.status==FINISHED. This might be useful in your paradigm as well.

Please also note that the actual presentation time of your stimuli is always a multiple of the duration of one frame. If your monitor uses the common 60 Hz, one frame will be 1/60*1000 = 16.66 ms long, and your fixation crosses will be 300, 316.66, … , 667.66, 700 ms long. For example, 510 ms would never be shown. Thus, you could also express the durations in frames and set them to 18-42 frames (at 60 Hz) instead of 300-700 ms if precise timing is important.

I hope this helps!

1 Like

setare.psyexp (10.5 KB) (12.6 KB)

Thank you so much. You did a great favor. Would you please kindly check my file to see what the problem is. I closely followed your instructions but still nothing.

I can’t fully test your experiment without the conditions file, but you need to change at least two things in your “textstroop” component:

  1. Set “Start” from “time (s)” to “condition” like in my example so that the code is interpreted as such
  2. Exchange fix.status==FINISHED with textfixationcross.status==FINISHED because that is the name of the fixation cross component in your experiment (and not just “fix” like in my example)

By the way, I highly recommend the update to PsychoPy 2020.2.10 because then you will get informative error messages again (their absence was a bug which is now fixed).

1 Like

Hello dear friend,

Thank you so much for your great help. The problem was solved.
The code ran at last.
But another problem came up. I have two separate routines and I should define this component code and fixationcross for that one as well.The first routine runs smoothly and correctly but when it comes to the second routine the program stops.
The first routine is surrounded by one loop and the second routine is surrounded by another loop.
I copied and pasted your instruction. I also paid attention to the naming.
But I wonder if I should determine another kind of coding for the second routine.

You will see the images first routine:


And you will find the images of my second routine:

I also set the same component “code” for both routines.

Should I define another kind of coding or setting for the other routine?

I really appreciate your great help.

Dear friend, thank you for your great favor. In the above post, I explained my problem. As you are more familiar with Psychopy, I would be thankful if you could help.

I’m glad it helped. :slight_smile:

The problem in your second routine probably occurs because you didn’t capitalize FINISHED. The spelling Finished is not defined. Correct spelling including capitals is very important in coding.

Please capitalize the word and install the update 2020.2.10. Download the zip file, click Tools / PsychoPy updates.. and install it. Afterwards, you will get error messages when the program doesn’t run, which makes bugfixing much easier.

I am truly grateful for your help. I really appreciate it. :pray:t2:

The problem was solved.

I will definitely update my software.



You’re welcome, glad I could help!