I need to randomly change the color of a stimuli in test trial I get: Error ("TypeError: Math.random.randint is not a function

Hello, I am new to Psychopy and my task is to create an online experiment and show 6 different color of squares at the same time. There will be a fixation cross at the center and squares will surround it. Next, after a break, only one of the squares will change color and participants should click to the square they think changed color. For the test trial I created the same squares I used in initial display trial( same colors from the spreadsheet and same position and to change only one square I thought I can add this at the top of the routine but I get ‘TypeError: Math.random.randint is not a function’ maybe this is not online compatible? what can I do instead?

Copy the colors from the spreadsheet
test_colors = [square_1_color, square_2_color, square_3_color, square_4_color, square_5_color, square_6_color]

Select a random square to change
square_to_change = random.randint(0, 5) # Randomly select one square (0-5)

Replace the chosen square’s color with the new color from the spreadsheet
test_colors[square_to_change] = random_square

Assign the updated colors to the squares
square1.fillColor = test_colors[0]
square2.fillColor = test_colors[1]
square3.fillColor = test_colors[2]
square4.fillColor = test_colors[3]
square5.fillColor = test_colors[4]
square6.fillColor = test_colors[5]

  • I guess instead of random.randint(0, 5) I should enter randint(0, 5)
    thanks a lot!
square_to_change = randint(6)

This should give a random integer between 0 and 5.

For the colours, I use e.g. square1.setFillColor(test_colors[0]) where the colours have been defined in a Both component.

Wow this is perfect! thank you so much! To make sure, this is how it should look right and only one color of the squares will change or should testsquares be from 1-6 :

If you want to set colours based on string names in your spreadsheet try:

square1.setFillColor(test_colors[0]) etc on the Python side and
square1.setFillColor(new util.Color(test_colors[0]); etc on the JS side.

Personally I would instead create a list of colours and then have numbers in the spreadsheet (if I use a spreadsheet for colours at all)

Hello, color sheet looks perfect but I am not sure if I am reading it. I see blue green red but these are the colors they cannot see right?

The blue, green and red in, for example, the Bright scheme should be distinguishable by someone with red-green colour blindness.

hello, the python column is the hues they would see without problem right? - I tried entering the numbers (like this: [-0.467,-0.067,0.333]) and the whole cell (like this: blue = [-0.467,-0.067,0.333]) but I get: * the argument should be an array of numbers of length 3– could you please clarify how to enter the right hues to the spreadsheet?

If you want to enter the numbers into a component try $[0.467,-0.067,0.333]

In a spreadsheet I would use an index number for a list of colours.

E.g. if I have
colours = [[‘blue’,blue],[‘cyan’,cyan],[‘green’,green],[‘yellow’,yellow],[‘red’,red],[‘purple’,purple],[‘grey’,grey]]

Then green would be 2 in the spreadsheet and I’d set the colour as

polygon.setColor(colours[rec1col][1])

hello, so if I am understanding correctly I should at first add as a code all the colors I will use like this. Then these will be selected from a spreadsheet from 0-19 let’s say. But there seems to be a problem with how I am entering the colors I guess:

Hello

yes that is a way to do it. but I would define the colours in a Begin Experiment tab and not a Begin routine tab. There is a superfluous comma in your last line of code.

Best wishes Jens

hi thank you, could you please clarify where is the superfluous comma in my last line of code because I am seeing the commas are the same in all. Also, this task will be the last task participants will complete, considering that should this still be in the begin experiment? thank you very much!

You are not understanding correctly. Please look at:

and

hi, I have looked at your color sheet and will use the colors there. Now checked the cribsheet and based on that I assume this is how I should be entering the colors? then would it work if I write the name of the colors here in the code to spreadsheet? please let me know if I am missing anything. thank you very much!

As @JensBoelte stated, this code should be in Begin Experiment, not Begin Routine, though it doesn’t matter too much if the routine is only used once.

I don’t think it will work if you put, e.g. blue_bright in your spreadsheet but if you add a line of code in an Auto routine (after this code has run) with something like

colours = [blue_bright, cyan_bright, …] (please type them all out.

Then you could have a column in your spreadsheet called colourIndex (or whatever you like) with a 0 when you want blue_bright

The you can use visualComponentName.setFillColor(colours[colourIndex])

Look here.

But you corrected it in the meantime.

Best wishes Jens

okay! so the code where I am defining the colors are in the begin experiment and in ‘both’ and at the very top of the routine.


Then, in another auto js code component again in begin experiment, I am defining the colors one by one:

and the routine looks like this :

I also added this :visualComponentName.setFillColor(colours[square_1_color,square_2_color,square_3_color,square_4_color,square_5_color,square_6_color]) to the second code component in begin routine but I am getting : visualComponentName is not defined error when I sync it.

and the spreadhsheet looks like this:

Try sq_1.setFillColor(colours[square_1_color])

sq_1 is a visual component name

Then try

sq_2.setFillColor(colours[square_2_color])

etc

1 Like

I can see the squares but for some reason they are all black:

Sorry – for you this should be sq_1.setFillColor(colors[square_1_color]) (American spelling of colour)

You could also add some print statements and check the developer console.

e.g.

print('sq_1',square_1_color,colors[square_1_color])

Hello, yes I entered as ‘color’ not ‘colour’, I do not think it is the problem:( if we enter the listed colors in psychopy in a spreadsheet it works perfectly, but when I enter them with the numbers like in your sheet or as the latest screenshot I attached, it doesn’t get what colors it should apply to I guess. Could there be another way? I mean, after I show them the squares they will see the same set, only one of them will change colors, so I was thinking a worst case scenario where I would create 50 set squares(total trial number) in properties but then would I be able to add a code that will change only one of the colors in the test trial?