# Generate random number & make a long stimulus name

Hey everyone,

I have a running experiment where two stimuli are presented on the left and right side of the screen. The following code does this and it works fine. The stimuli are in an excel sheet contains the target and distractor image paths.

if random() < 0.5:
targetPos = [-0.5,0]
otherPos = [0.5,0]
corrAns = ‘a’
else:
targetPos = [0.5,0]
otherPos = [-0.5,0]
corrAns = ‘l’

My supervisors told me that it’s better to have PsychoPy randomly choose a target so that the two images are not always chosen together. He said this can be done by generating a random number from 1 to 10 to create a long stimulus name.

I am new to coding so I’m not sure how this should go but I am thinking:

#get random number from 1 to 10
import random
for x in range(10):
random.randint(1,10)

Then to make the string would you use %s? and then use %x ? Also, once the string is generated, what would I have to change for PsychoPy to use this code?

@himynameisclara, are your stimuli files named “10.png” etc?

Yes, they are currently called Pointy1.png or Round1.png … and so on with the path being ./EndorsedStimuli/Round/Round1.png

Ok great. Well, your code looks fine, but if you are using Builder, `randint` is already imported. So, in a code component you could just use:

``````randint(1,10)
``````

To format your filename strings, you could use

``````fileName = "round{}.png".format(randint(1,10))  # Puts your new filename in a variable
fileName = "pointy{}.png".format(randint(1,10))  # Puts your new filename in a variable

print(fileName)  # Just so you can see it
``````

Thank you very much! I see that it works with generating the random filename strings

So in the builder, what should I replace so it picks the files with the generated names?

Thanks a lot!!

Well, you will want to set your images before each trial. Before we do that, are you only having stim of type round and pointy and only have two folders to choose from? Also, how is it determined whether the round or pointy are on the left of right of the screen? If you have that sorted, then just add the following to the “Begin Routine” tab

``````path = "./EndorsedStimuli/Round/"
fileName = "round{}.png".format(randint(1,10))
# Now you have to set your image
yourImageComponent.setImage(path+fileName)
``````

You will have to do this for both of your stim, on each side of the screen.

The excel sheet contains two columns for the Target and the Other. Half of them are round and half pointy. The code I wrote before determines their position.

day1.xlsx (9.2 KB)

Ok, well as you have all your stim already in a spreadsheet, perhaps a better idea would be to shuffle around your columns and create a new spreadsheet for your conditions file. You can ignore the previous suggestion, and try the following. In your “Begin Experiment” tab:

``````import pandas as pd

# Get trials

# Split trials into separate dataframes
targets = trialFrame[["target", "typeTarget"]]
others = trialFrame[["other", "typeOther"]]

# Shuffle one of the dataframes
others = others.sample(frac=1).reset_index(drop=True)

# Merge shuffled dataframes back together
finalTrials = pd.concat([targets, others, trialFrame['wav']], axis=1)

# Write to new conditions file
finalTrials.to_excel("newDay1.xlsx")
``````

Now, you will just need to change the name of the conditions file in the loop handler to “newDay1.xlsx”. Then, you can access your variables as normal, e.g, use `\$target` in your image components.

thank you so much! seems like it’s working great