# Vector/list of random permutated values

**URL:** <https://mworks.discourse.group/t/vector-list-of-random-permutated-values/1013>\
**Category:** Support\
**Created:** [July 10, 2024, 5:20pm UTC](https://mworks.discourse.group/t/vector-list-of-random-permutated-values/1013 "2024-07-10T17:20:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcadena](https://avatars.discourse-cdn.com/v4/letter/j/76d3ee/32.png) [@jcadena](https://mworks.discourse.group/u/jcadena)\
**Post date:** [July 10, 2024, 5:20pm UTC](https://mworks.discourse.group/t/vector-list-of-random-permutated-values/1013/1 "2024-07-10T17:20:36Z")

</div>

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](https://mworks.discourse.group/uploads/short-url/5Z7HKtl3SbQ4VcuOSPpGdgAhXfp.mwel) (6.0 KB)

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [July 11, 2024, 2:40pm UTC](https://mworks.discourse.group/t/vector-list-of-random-permutated-values/1013/2 "2024-07-11T14:40:16Z")

</div>

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:

```auto
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:

```auto
var rand_grid_index = [0:440]

```

Then, use Python’s [random.shuffle](https://docs.python.org/3/library/random.html#random.shuffle) function to randomize it:

```auto
// 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](https://mworks.discourse.group/uploads/short-url/clyvuDD0wVyFIiryHHgPNQ6EicW.mwel) (6.1 KB)  
[Stimulus\_color\_coherence\_CJS\_python.mwel](https://mworks.discourse.group/uploads/short-url/8fi7Vqj3L8uHOiTSAl94KrlGZrF.mwel) (5.9 KB)

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [July 11, 2024, 2:40pm UTC](https://mworks.discourse.group/t/vector-list-of-random-permutated-values/1013/3 "2024-07-11T14:40:29Z")

</div>

Made this topic public on July 11, 2024, 2:40pm UTC



---

<div class="post-metadata">

**Author:** ![jcadena](https://avatars.discourse-cdn.com/v4/letter/j/76d3ee/32.png) [@jcadena](https://mworks.discourse.group/u/jcadena)\
**Post date:** [July 12, 2024, 12:31pm UTC](https://mworks.discourse.group/t/vector-list-of-random-permutated-values/1013/4 "2024-07-12T12:31:03Z")

</div>

Thank you very much!

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [July 16, 2024, 1:29pm UTC](https://mworks.discourse.group/t/vector-list-of-random-permutated-values/1013/5 "2024-07-16T13:29:00Z")

</div>

Closed on July 16, 2024, 1:29pm UTC


