How to present a sound randomly in between trials?

I am trying to build an emotional dot motion task in builder. I have created a simple loop and a condition file in which my stimuli are presented and the participants answer each trial with left or right keys and it works fine. But i also have a sound file (a siren sound) and i want it to be played only four times randomly in between trials. Is there any way to do this?
Any help would be appreciated.

Please could you describe the trial sequence should it be:

trial (left/right judgement) > 4 sirens with set intervals (presented on 50% of trials for example) >trial left/right) > 4 sirens with set intervals (presented on 50% of trials for example) and so on?


You could set up a routine that plays the sound after every trial. Then use a code component to skip that routine except on your key values of trials.thisN.

For example, you could set up

Begin Experiment

sirenTrial = randint(4)+3

Begin Routine (siren routine)

if trials.thisN == sirenTrial:
     sirenTrial += randint(4)+3
     continueRoutine = False

thank you for replying…what i wanted to do was to present the subjects with 60 trials of dots and i also had a siren sound that played only four times and each time for 5 second randomly after the dots. for example:

dots (20 trials) > siren (1st time) > siren (2nd time) > dots (15 trials) > dots (10 trials) > siren (3rd time) > dots (15 trials) > siren (4th time)

What i did 4 days ago was to create 12 inner loops (8 dot loops and 4 siren loops) and a randomizer outer loop:

dot loops (dots_1 = 6 trials, dots_2 = 10 trials, dots_3 = 4 trials, dots_4 = 6 trials ,dots_5 = 8 trials, dots_6 = 6 trials, dots_7 = 6 trials, dots_8 = 14 trials) = 60 trials

siren loops ( siren_1 to siren_4 = 1 trial each) = 4 trials

randomizer outer loop = present a random combination of inner loops for each participant.

then i randomized my inner loops using the method described in the link below:

and it’s now working i expect.

I also tried this code and it plays the sound routine every 4 to 7 trials randomly which is more than the 4 time limit for the sound routine, but what i wanted to do was to present the sound routine only 4 times during the whole loop.

My proposal involves a single loop. I assumed you wanted the positions to be random but if you want them after trials 20, 35, 45 and 60 then try this.

Begin Experiment

sirenTrials = [19,34,44,59]

Begin Routine

if trials.thisN not in sirenTrials:
     continueRoutine = False