Jessica C
Jessica C

Reputation: 31

How to pick a random number in a range - + convert to ms

I'm trying to figure out how to generate a random number so that my stimulus will appear on the screen for a random time between 1200 and 2200 ms. I've figured out that random.randomint (1200,2200) will generate the number I'm looking for.

However, this gives me a time in seconds, not milliseconds. If I divide the number by 1000, I assume it will still give me either 1 or 2 because those are the rounded integers.

I'm also looking for some guidance on how to insert better code for keyboard input. It will eventually be working up to a stroop test.

from psychopy import visual, core
import random
import time
import datetime
import sys
from psychopy import event



file = open ("Test Output.txt", 'w')


win = visual.Window([800,800],monitor="testmonitor", units="deg")



for frameN in range(5):
    MyColor = random.choice(['red','blue','green','yellow'])
    Phrase = random.choice(["Red","Green", "Blue", "Yellow"])
    time = str(datetime.datetime.now())
    key = str(event.getKeys(keyList=['1','2','3','4','5'], ))
    pause = (random.randint(1200,2200)/1000)
    msg = visual.TextStim(win, text=Phrase,pos=[0,+1],color=MyColor)
    msg.draw()
    win.flip()
    core.wait(pause)

    data = "Color:"+ MyColor + "     " + "Time:" + time + "    " + "Text:"+ Phrase + key 

    file.write(data  + '\n')


file.close()

Upvotes: 1

Views: 2226

Answers (1)

mvelay
mvelay

Reputation: 1520

Use float division as follows:

pause = random.randint(1200, 2200) / 1000.0 # time in seconds

Upvotes: 2

Related Questions