Reply To: Conditional Randomization

PennController for IBEX Forums Support Conditional Randomization Reply To: Conditional Randomization


Hi Leo,

Playing with the order of trials is probably Ibex’s least handy aspect. If you look up Ibex’s documentation manual, you’ll see that the function rshuffle in part does what you want: it makes sure that specifically labeled trials are evenly spaced. Say you have 6 trials in your experiment, two labeled typeA, two labeled typeB and two labeled typeB. Using PennController.Sequence( rshuffle("typeA", "typeB", "typeC") ) will give you one of these six possible patterns:

  • typeA, typeB, typeC, typeA, typeB, typeC
  • typeA, typeC, typeB, typeA, typeC, typeB
  • typeB, typeA, typeC, typeB, typeA, typeC
  • typeB, typeC, typeA, typeB, typeC, typeA
  • typeC, typeA, typeB, typeC, typeA, typeB
  • typeC, typeB, typeA, typeC, typeB, typeA

I understand your request to be slightly different though. You don’t necessarily want to define a specific pattern that should repeat itself, you would be okay with having C, B, A in the first block and A, B, C in the second block as long as, say, no C follows an A directly. Am I right3

One of the problems with implementing a general solution to such a request is that, depending on how many trials you have per label, what you ask may or may not be feasible. So you need to make sure, as the designer, that your set of trials are labeled in a way that makes it possible to create a sequence with the desired properties. Then you could define this function at the top of your script:

function RandomizeExcludeSequence(ar, type1, type2) {
  this.args = ar; = function(arrays) {
      let sequence = arrays[0];
      let shuffle = true;
      while (shuffle){
        shuffle = false;
        let prev = "", next = "";
        for (let i = 0; i < sequence.length; i++){
          if (i>0) prev = sequence[i-1][0].type;
          next = sequence[i][0].type;
          if (prev==type1&&next==type2){
            shuffle = true;
      return sequence;
function randomizeExcludeSequence(ar, type1, type2) { return new RandomizeExcludeSequence([ar], type1, type2); }

and use it like this:

PennController.Sequence( randomizeExcludeSequence( anyOf("typeA", "typeB", "typeC") , "typeA", "typeC" ) )

PennController("typeA", newButton("typeA i").print().wait() )
PennController("typeA", newButton("typeA ii").print().wait() )
PennController("typeB", newButton("typeB i").print().wait() )
PennController("typeB", newButton("typeB ii").print().wait() )
PennController("typeC", newButton("typeC i").print().wait() )
PennController("typeC", newButton("typeC ii").print().wait() )

This generates any random sequence using all the trials labeled typeA, typeB or typeC that does not contain any pair of trials successively labeled typeA then typeC.