Vector/list of random permutated values

Hi Chris,
This is Jaime from Fribourg. I have a short question. I’m programming a task where I first display some squares. After some time, a random proportion of them become transparent. The idea is to change the color coherence of the stimuli (i.e., 80% are blue, 20% are gray, then 75% blue, the rest gray and so on). (see attached code).
To generate such behavior, I used a selection variable to generate random numbers from a range without replacement. However, for some reason, this doesn’t work for low coherences (in my example code, there should always be just 4 - 5 squares appearing). Therefore, I thought it would be simpler if I could generate a list of randomly permutated integers like the ranperm function in MATLAB. Is there an equivalent function or a way to generate this kind of randomization?
Thanks in advance.
Jaime
Stimulus_color_coherence.mwel (6.0 KB)

Hi Jaime,

However, for some reason, this doesn’t work for low coherences (in my example code, there should always be just 4 - 5 squares appearing).

This is due to an error in your selection variable. You have 441 stimuli, but the selection variable contains values 0 to 441 (442 values total). The correct range should be 0 to 440. (The first time rand_grid_index takes the value 441, you append a new value to the list in grid_alpha, but since no stimulus uses that value, it doesn’t make anything transparent.)

Fixing this reveals another issue: Before starting the loop where you make random stimuli transparent, you need to reset the selection variable:

reset_selection (rand_grid_index)
while (stim_index < ( num_stims*(1-grid_coherence) ) ) {

If you don’t, the first time you run your experiment, you’ll see that 4 of the 5 stimuli that are displayed when grid_coherence is 0.01 will be in the exact same spots as the “holes” you see when grid_coherence is 0.99.

I’ve attached a modified version of your experiment that contains these changes.

Therefore, I thought it would be simpler if I could generate a list of randomly permutated integers like the ranperm function in MATLAB. Is there an equivalent function or a way to generate this kind of randomization?

You could do this with a little Python code. First, make rand_grid_index a normal variable with a list value:

var rand_grid_index = [0:440]

Then, use Python’s random.shuffle function to randomize it:

// Change randomly n squares to comply with coherence
stim_index = 0
run_python_string ("import random; rgi = getvar('rand_grid_index'); random.shuffle(rgi); setvar('rand_grid_index', rgi)")
while (stim_index < ( num_stims*(1-grid_coherence) - 1 ) ) {
    grid_alpha[rand_grid_index[stim_index]]=0
    stim_index += 1
}

I’ve attached another version of your experiment that uses this approach.

Cheers,
Chris
Stimulus_color_coherence_CJS.mwel (6.1 KB)
Stimulus_color_coherence_CJS_python.mwel (5.9 KB)

Thank you very much!