Howdy, Stranger!

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

Supported by

Variable ISI - javascript equivalent for python random.randint()?

Hi,

I would usually have a random (constrained) fixation screen duration between stimuli (ISI).

In OpenSesame this works (e.g. for fixation duration approx 1000ms, varied between 750 and 1250ms):

import random

var.fix_duration = random.randint(750, 1250)

Does anyone have examples of how to convert this to OSWeb Inline in order to use it in an online experiment?

Thanks,

Carolyn

Comments

  • Hi @carolyn.

    In javascript most of these functions are located in the Math library. the function Math.random() returns a value between 0 and 1, so you could translate your statement as such:

    vars.fix_duration = parseInt(750 + Math.random()*500)
    

    I hope this helps.

    Buy Me A Coffee

  • Hi Daniel,

    Thank you for the quick response, that works!

    Cheers,

    Carolyn

  • Hi,

    I have a simlar query. I'm converting an experiment to work on OSWEB. I have a variable ISI that ranges between 500-1500 with steps of 100ms.


    The current script is as follows.



    I am not a coder and get buy by finding other peoples codes and modifying where necessary. Any help would be gratefully received.


    Best wishes


    Deiniol

  • Hi @Skilli

    The stepping makes it more difficult. Unfortunately javascript doesn't have a random.choice like function, so you will have to do something like:

    var baseISI = 500
    var increments = 100
    var step = parseInt(Math.random()*10)
    vars.ITI = baseISI + increments*step
    

    That should do the trick.

    Buy Me A Coffee

  • Thanks @Daniel

    This makes alot of sense to me along with the text above. I had to declare the variable ITI at the start of the experiment (see image)


    However, it does not work. I get an invalid duration error message (see image)



    Here is how the code looks in the experiemnt.


  • Hi @Skilli

    You need to place this code in the prepare phase while you put it in the run phase.

    Buy Me A Coffee

  • Good man! Thanks you @Daniel that worked. Just one question, why does code have to go in the prepare phase and not the run phase. From what I understood, declaring variables go in the prepare phase and running code goes in run, as it runs.

    Please excuse stupid questions, I'm trying to learn.


    Thanks again


    Deiniol

  • edited September 2020

    Hi @Skilli,

    The long explanation you can find here: https://osdoc.cogsci.nl/3.3/manual/prepare-run/

    The short explanation: everything in the prepare phase section of script items is executed before the trial starts (i.e. the first display is shown). This is to make sure that potential time consuming calculations do not mess up the timings of the displays when they are shown. So generally, all code that is resource intensive and may take some time to execute should be placed in the prepare phase. Because all other items beside inline scripts also have this prepare phase in which they set themselves up to run, they need to have information about duration etc. already in this phase, and therefore you needed to define the ITI in this phase too.

    Code in the run phase is executed during a trial run. Generally you only place code here that involves the actual presentation of the displays (e.g. `canvas.show()`) or code that is dependent on the response of the participant.

    I hope this helps! (and thanks for the caffeine shot ;) )

    Buy Me A Coffee

  • Thanks @Daniel. Does that means inline script using the prepare phase can go anywhere in the trial sequence?

  • Basically yes. Within a sequence, all prepare phases are executed before all the run phases. So as long as each of the prepare phases does not rely on stuff happening in future prepare phases or any of the run phases, it can go anywhere in the sequence.

    Of course there are some obvious mistakes to make (e.g. using the user response in a prepare phase), but these are usually quickly found.


    Good luck,

    Eduard

    Buy Me A Coffee

Sign In or Register to comment.