How to make marker on rating scale (0 to 10) start from random number

Hi everyone, this is probably basic stuff, I am just new to Psychopy and need some help with making the marker of the rating scale start at random on every trial.

**PsychoPy version 3.2.4:
** I have a Rating scale with numbers from 0 to 10, with the option ‘hover’. All the numbers are white and the mouse-selected number turns orange. The markerStart is set to 5 now (see below). However, I would like to set it to have a random start on every trial.

rating = visual.RatingScale(win=win, name=‘rating’, marker=‘hover’, size=1.0, pos=[0.0, -0.5], choices=[‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’, ‘10’], tickHeight=-1, markerStart=5)

**I tried to include a code component defining the Marker start as: scale_start = randint(0, 10), and then adding $scale_start at the ‘Marker start’ option on the Rating scale component.

**But if I include this at ‘Begin routine’, the random number is set only once and is used for all trial (e.g. marker start is 7 every time). And if I include this in ‘Each frame’, I get: NameError: name ‘scale_start’ is not defined.


I gave this a try and added an additional line to your code to write the starting position of the marker to your data file. Note that I had to change the rating type to triangle (others might work as well), because it does not seem to work visually with ‘hover’ (it does run for me, though).

# set start value for marker
randStart = randint(0, 10)
# write to data file
thisExp.addData('marker_start', randStart)
# initialize rating scale
rating = visual.RatingScale(win=win, name='rating', marker='triangle', size=1.0, pos=[0.0, -0.5], choices=['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10'], tickHeight=-1, markerStart=randStart)

Make sure the code component is positioned above the rating component in the trial. The code snippet should be added in the ‘Begin Routine’ tab.

Good luck!

test.psyexp (7.0 KB)

1 Like

@Tweads This actually worked perfectly with ‘hover’ as well. Saved me so much time. Thank you!

1 Like


Online, is it possible to do something like that (randomize the initial position of the marker) with the slider instead of being invisible until the participant click?

I have tried this:

# set start value for marker
randStart = randint(0, 10)
# write to data file
thisExp.addData('marker_start', randStart)
# initialize slider
slider = visual.Slider(win=win, name='slider',
    size=(1.0, 0.1), pos=(0, 0), units=None,
    labels=None, ticks=(0,1),
    granularity=0, style=('triangleMarker',),
    color='LightGray', font='HelveticaBold',

But I have the followed problem: * ReferenceError: Can’t find variable: thisExp

Hello! I am following up on this question. I was able to implement this code to get a random start value generating; however, similar to the individual who asked the question, the same random number is being used for all of my trials. Is there an additional line of code that is needed to ensure a new random number is generated for each trial?

Hi Ralie, How did you make it so that the experiment would generate a new marker start value for each trial? My code is leading to the same thing you initially mentioned (same random start value for each trial)

So, I wasn’t able to solve it in the end. The marker was still only appearing upon the first click. But one thing you can try is add a mouse response components, and inside Begin routine: rand = randint(1,10); And then inside Every frame:

if buttons == 0:
    slider.markerPos = rand

The key to make the marker appear before the mouse click is: slider.marker.draw().
Unfortunately, this is not a full solution, because the marker returns to the beginning position once the mouse is released. Hopefully you can solve this.

Hi @ralie_kostova ! I was able to randomize the slider marker for each unique trial. The key is to put your code in the ‘begin routine’ tab.

I used this code:

Note, some parts of the code will change depending on which slider type you use (ex: rating vs slider) my experiment used the slider.