| Reference | Downloads | Github

How to randomize 8 trials that are repeated 10 times in one block

If this template helps then use it. If not then just delete and start from scratch.

OS (e.g. Win10): Win8
PsychoPy version (e.g. 1.84.x): 1.90.1
Standard Standalone? (y/n) y
What are you trying to achieve?: trial randomization

I do use 2 different sound files and use 2 lines of stimtracker to deliver DC pulse for haptic stimulator. Participants’ response will be collected via voice-key.

In my experiment, I have 8 different types of trials in one block.
So 4 trials are unimodal and remaining 4 would multimodal.
In the block, each trials should be repeated exactly 10 times.

Therefore, there will be 80 trials in one block and I want to randomize these 80 trials.
Here are details for components in different types of trials

Trial 1-2: 1 sound component with 1 code component for voice key (Sounds files between trial 1 and 2 are different)

Trial 3-4 : 1 code component for pyxid with 1 code component for voice key (Code component for pyxid activates different lines between trial 3 and 4).

Trial 5,6,7,8: 1 sound component, 1 code component for pyxid, and 1 code component for voice-key (Trial 5,6,7,8 have its own combination).

What did you try to make it work?:
I looked at Demo for trial and blocks but I do not know how to apply demo for my experiment. Demos have different image files in the one folder. But my experiment doesn’t fall into that setting, especially for pyxid-stimtracker.
First, I uploaded cvs files for each trial that has its own condition file with 10 different labels.-For each condition, I found that sequential or random do not have any effect because I repeat it 10 times. Then I tried to loop all 8 trials under block with fullRandom.
Second, I did another way. each trial has its own condition with only with 1 label. Then I tried to loop them with 10 conditions of the block. This method also didn’t work.

What specifically went wrong when you tried that?:

Include pasted full error message if possible. “That didn’t work” is not enough information.

I did not have error message.

Thank you so much for your comments /advice in advance.



I’m not sure if I understood your description correctly. Please let me know if I didn’t.

So, if I understood it correctly, you might want something like this:

  • A single trial routine
  • An input file like this:
  • A loop that repeats this file 10 times (random or fullRandom, depending on what you prefer)

The general approach is to use dummy sounds that are unhearable and dummy StimTracker lines that are unfeelable. This would allow you to use a single trial routine independent of your condition.

Please note that I have never used sound files nor StimTracker lines, so I don’t really know if the approach would work.

Hope this helps.


Dear Jan

Thank you for your advice.
I tried your suggestion, but it seems not working.
I feed in both sound and lines into Stimtracker who controls timing. Stimtracker got confused with this dummy stimuli and delivers both sounds in every single trial and sometimes dropout lines.
Do you have any other suggestion?


As I haven’t used haptic stimulation, I don’t really know how these stimuli are generated and how you could create a dummy stimulus. Could you explain how exactly you implemented my suggestions? Alternatively, feel free to upload your experiment and your input file.

In any case, your main experimental trial should have only one sound component. So, it’s not really clear to me why it would deliver both sounds in every trial.


Dear Jan

Thank you for your comment.

I created cvs file that are exactly corresponding to your suggestion. For dummy stimuli, I created code components whose labels are d_A(dummy sound), & d_T(dummy touch). However, they don’t have any contents in the script.

I am doing psychophysics experiment so precise-timing is critical. Frankly speaking, I would like to avoid, especially, preloading dummy (slient) sound files for my dummy-sound because it causes imprecise timing.
For my sound-touch(stimtracker) coupled stimulus, I want them in sync. Somehow psychopy introduces delays in sound presentation about 130ms (frame N between 4 & 5). So I had to delay my onset of stimtracker activation. There is also variability of SOA between sound and stimtracker. We registered to oscilloscope in order to see timing. That is why my dummy code components are empty.

In order to reduce confusion about my haptic stimulation: I let psychopy activate two different lines of stimtracker by using the commands (please see the pic). According to my understanding, these ‘activate_line’ commands to deliver the DC pulse. Stimtracker sends out DC pulse to my biopac haptic stimulator. Then my haptic stimulator responds to the DC pulse.

When I put each trial separately in one routine, there is no confusion in stimtracker and it delivers stimulus correctly. Is there any other way randomize these trials each of which has its own routine??

Thank you in advance!!!


Ah, that routine doesn’t look quite right. You should have only one sound component and one code component controlling the haptic stimulation. You might want to have a look at some of the PsychoPy videos on YouTube and some of the demos that come with the Builder to learn how to use a conditions file to control stimulus presentation.


Dear Jan

When I have one sound component and one code component in one routine. Psychopy never randomizes it(see the pic)


In demos, one routine has multiple trials and lists of cvs file are used for randomization.
In my case, one routine has one trial. Due to voice-key recording, each routine has 1 condition file. Then I looped all 8 trials in a block and loaded your suggested condition list. If this is case, psychopy doesn’t randomize it.

Do you have any idea for this??


No, all you need is a single trial routine (i.e., “trial” in your example) and a single loop (“trials” in your example). The input file determines what sound/haptic stimulus is presented (i.e., you shouldn’t have an “LA” component, but just a general sound component and you should let the input file determine which particular stimulus is delivered). And the loop looks after the randomisation. Again, please have a look at the YouTube videos/the demos with input files.


1 Like

Hello, I watched the demos and now I able to make input files for sound stimulus. But I wonder how I can upload different code components as an input file.

I hope you can give me some hints for me.


Sorry, I’m not quite sure what you mean by “how I can upload different code components as an input file”. You don’t usually add code components to an input file, these are part of a routine.


Dear Jan

Thank you for reply.
To drive two different types of haptic stimulus, I have not one but more than 2 code components (I have different codes for haptic stimuli via stimtracker).
If I cannot upload different code components with an input excel file, then I cannot run my experiment in a single routine. My question was, whether I can upload txt or py file for the code components. Or, is there any way to randomize multiple routines?

Thank you in advance!

The idea would be to specify a certain parameter in the input file that gets then used by the code component. For example, this could be the length and/or amplitude of the stimulation.


Thank you Jan.

That might be complicated than I thought.

According to your reply, it seems that I should consider line numbers as a parameter. For my right haptic stimulus, I use line 2 and for the right I use line1. I know there are 8 lines in total but all of them always deliver pulse. So There is no parameter value for my touch dummy. According to my testing, if I assign other values than integer between 1 & 8, psychopy gives me error message.

Second problem is timing. has a considerable amount of time delay. Therefore I set specific frame numbers for the code components for stimtracker in my sound-touch multimodal condition. This is another reason that I do not want to play silence for my dummy sound.

Is there any way I can randomize multiple routines??



As I’m still not a 100% sure what type of randomisation you would like to achieve: Is it correct that you would like your conditions to be randomised (randomor fullRandom in PsychoPy), or do they need to occur in a specific order?

With regard to haptic stimulation: So, what happens when you use line 3? As mentioned before, I’ve never used haptic stimulation, but I would suspect that you can specify things like amplitude, frequency, and length of stimulation, all of which could in theory be suitable parameters to manipulate.

As I’ve never used sound stimuli, I don’t really know anything about potential delays and how to correct for them. However, if you do need to correct for delays and if it is possible to accurately correct for delays, I don’t quite see how a dummy sound would be an issue (but an actual sound in a multimodal condition would not be an issue).


Dear Jan

I am sorry that my description is not clear.
I want to randomize 8 routines(Not trials). They should be repeated 10 times. So I want 80 routines to be presented randomly.
I looped 8 routines with random or fullRandom with 10 responses in PsychoPy but stimulus presentation was always sequential from routine 1 to 8.

For the haptic stimulation, when I used line 3, it activates the stimulator that I am using. The other remaining lines are the same. From my examination of stimtracker, dummy haptic presentation seems impossible. I don’t specify the amplitude or frequency or stimulation duration. I need stimulation of hands left or right hands.

For sound, I measured the sound presentation latency through oscilloscope. If psychopy has the fixed latency for sound presentation, your suggestion can be taken. However, the latency duration varies that I cannot control. I know this variance because I set my stimtracker presentation as my reference. I checked all different combination of audio lib (pyo, pygame, sound devices) and driver(primary sound, ASIO, Audigy, etc.). None of them has the fixed latency.

I hope this gives you clear idea on what I want to achieve.

Thank you!


I’m afraid I’m still a bit confused. Usually, a routine in PsychoPy represents what I would call a trial in a psychological experiment…

With regard to the haptic stimulation: If it’s problematic to set up a dummy stimulation, and as you seem to be using a code component for the haptic stimulation, you could alternatively do the following:

  • Define arbitrary conditions in your input file (e.g., 1, 2, and 3, or left, right, dummy)
  • Then use if and elif in your code to distinguish the different conditions and simply don’t run the haptic stimulation code for the dummy condition