Howdy, Stranger!

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

Supported by

showing images randomly from the same category

edited March 2021 in OpenSesame

Hello everyone!

I've been using Open Sesame for a while now, so I've decided to start programming my experiment.

Firstly, I uploaded a bunch of different images which can actually be grouped into six different categories, and I expect that Open Sesame would randomly display one image from each category.

For the sake of clarity, I uploaded 5 different happy faces, which I named as happy-1, happy-2, happy-3, happy-4, and happy-5. I would expect that one of them would be randomly picked everytime I called for the happy condition to be showed.

Thus, I was thinking of coding something like that:

draw image center=1 file="[category_name[random1,5]]". 

It does not work, though.

What would you suggest that I do?

I would really appreciate if you could help me

Comments

  • Hi @MicolG ,

    I would start by adding a short inline_script at the start of the trial sequence where you determine the file name based on the category_name variable and a randomly selected digit between 1 and 5 (inclusive). This should be in the prepare phase.

    import random
    var.my_image_file = '{}{}'.format(var.category_name, random.randint(1, 5))
    

    And then you can use the variable my_image_file in the script of your sketchpad :

    draw image center=1 file=[my_image_file]
    

    Do you see the logic? And note that the two scripts use a different language: Python for the inline_script and OpenSesame script for the sketchpad script.

    -- Sebastiaan

  • edited March 2021

    Thank you @sebastiaan , I definetely got your point.

    Could you tell me what syntax should I use for OSWeb (javascript)?

  • Hi @MicolG ,

    In JavaScript (i.e. an inline_javascript item) the logic is the same, but the details of the implementation are a bit different. Specifically, there is no randint() function in JavaScript, so you need to implement it yourself.

    function randomInteger(minval, maxval) {
        return minval + Math.floor(Math.random() * (maxval - minval + 1))
    
    }
    vars.my_image_file = vars.category_name + randomInteger(1, 5)
    console.log(vars.my_image_file)
    

    —Sebastiaan

  • Thank you so much,

    You've helped me a great deal!


    MG

Sign In or Register to comment.