# Randomization with constraints

**URL:** <https://discourse.psychopy.org/t/randomization-with-constraints/7482>\
**Category:** Coding\
**Created:** [April 20, 2019, 4:01pm UTC](https://discourse.psychopy.org/t/randomization-with-constraints/7482 "2019-04-20T16:01:18Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cijay](https://avatars.discourse-cdn.com/v4/letter/c/8e8cbc/32.png) [@Cijay](https://discourse.psychopy.org/u/Cijay)\
**Post date:** [April 20, 2019, 4:01pm UTC](https://discourse.psychopy.org/t/randomization-with-constraints/7482/1 "2019-04-20T16:01:18Z")

</div>

Good day,

I’m essentially running a series of 6 memory tests for each participant, 3 of which the participant studies and is tested on words displayed on the same background image (repeat) and 3 of which the background at study and test differs (mismatch). Using the builder, I have created this experiment. The problem is with the randomization.

I have a total of 9 background images (e.g. image1.jpeg, image2.jpeg, image3.jpeg, etc.). I’m trying to randomize them such that:

1. They are all used.
2. Three of them are randomly selected to be repeated at study and test.
3. The rest of the remaining 6 images are used for the mismatch blocks.

To be more concrete, here are some fabricated examples of randomizations that would fit the constraints (and do work with the experiment I built when they are set up as an excel conditions file):

Example1

Background1 Background2 ContextType  
image1.jpeg image1.jpeg repeat  
image2.jpeg image2.jpeg repeat  
image3.jpeg image3.jpeg repeat  
image4.jpeg image5.jpeg mismatch  
image6.jpeg image7.jpeg mismatch  
image8.jpeg image9.jpeg mismatch

Example 2

Background1 Background2 ContextType  
image6.jpeg image6.jpeg repeat  
image1.jpeg image1.jpeg repeat  
image9.jpeg image9.jpeg repeat  
image7.jpeg image5.jpeg mismatch  
image2.jpeg image4.jpeg mismatch  
image8.jpeg image3.jpeg mismatch

Now, I’m sure something like this can be done at the start of the experiment through the code component, but since I’m a novice with Python, I’m not quite sure where to start. Any advice on how to go about doing this would be greatly appreciated!
