Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

Constraining pseudorandomisation to a maximum distance

edited June 2018 in OpenSesame

Hi all,

I am programming a variation of the n-back task. I am presenting a string of 16 letters (using a loop with the variable, letter). 12 of the letters are unique and four are repeated.
In the 1-back condition, the repeated letters must be presented immediately after the first appearance of the letter.
In the 2-back condition, the repeated letters must be presented 2 letters after the first appearance of the letter.
In the 3-back condition, the repeated letters must be presented 3 letters after the first appearance of the letter.

I am hoping to randomise the order of the presentation of the letters but need to constrain the presentation of the repeated letters.
There is the constrain variable mindist function, which can set a minimum distance for the repeated letters. Is there an equivalent maxdist? If so, I can set mindist and maxdist to the same value.

If not, how can I ensure the presentation at the correct number of letters?

Attached is a picture of how my experiment currently looks (the letters are assigned in the set_stims_order inline scripts).

Thank you in advance for any help people can provide.



  • Hey,

    I do not know about the maxdist function but just one question can the first presentation of a repeated letter happen successively, like:

    ABCAB... (for the 3 back here for instance)?



  • Hi Sylvain,

    Thank you for your response.

    Yes, the different presentations can be intermixed as you have described.


  • Hello Kyran,

    I think this should work.
    Look at this example for the 3-back:

    import random
    letter = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L']
    n_back = 3
    # define the possible slot where the repeated item can be
    all_slot_possible_repeat = range(n_back, 16)
    # define all slot possible
    all_slot_possible_all = range(16)
    #define the final list of stims
    var.list_final = ['NA'] * 16
    for i in range(4):
        # select a slot randomly in all_slot_possible
        element_repeat = random.choice(all_slot_possible_repeat)
        # remove this slot from the list all_slot_possible_repeat and all_slot_possible_possible
        # now for the first presentation
        element_first = element_repeat - n_back
        if element_first in all_slot_possible_repeat:
        # now fill var.list_final
        var.list_final[element_repeat] = letter[0]
        var.list_final[element_first] = letter[0]
        # remove the letter
    # now fill the remaining letters
    for pos, value in enumerate(var.list_final):
        if value == 'NA':
            var.list_final[pos] = letter[0]

    Also a script attached to show you how to populate your loops.

    I hope it helps!


    Thanked by 1KyranTGS
  • Sorry for getting back to you so late but thank you so much for doing that, Sylvain.

    I will have a look now to see how it works for me.

  • Brilliant, that does precisely what I need it to.

    Thank you very much for your efforts, I really appreciate it.

Sign In or Register to comment.