Get random numbers 1 and 2 online (for counterbalancing)

Hey everyone,

I just need to get a random number between 1 and 2 to assign my participants to their respective conditions.

I saw that random() was not working online, but math.random() does as it was mentionned on a previous post.

How do you actually implement it? I tried this for instance both the lines on the image, but they always say in the end that randomNumber is not defined (online).

What is there to change?

Thank you!

Alexane

Hello,
You can try using shuffle():

# Before Experiment:
randomNumber = [1, 2]

# Begin Routine:
shuffle(randomNumber)
chosenNumber = randomNumber[0]

It should easily auto-translate to JavaScript.

You can take a look at the Crib Sheet for more methods:

Chen

1 Like

Hello,

random() should auto-translate properly PsychoPy to PsychoJS without the need to define in it a JS-code-component at the experiment begin with the current version of PsychoPy. So, the following code generates a random number between 1 and 2 (tested locally in a browser):

rand = random() + 1

In older versions of PsychoPy, you needed a JS-only code-component and defined at the begin of the experiment the function.

Best wishes Jens

1 Like