Random Number Generator help

740 views
Skip to first unread message

BaileyMan

unread,
Feb 9, 2011, 7:29:10 PM2/9/11
to psychopy-users
Hi,

I know very little about programming, and pretty much nothing about
python. I would love some assistance with something that's probably
straight forward for someone who knows what they're doing. I could
probably figure part of it out with enough time invested, but I
thought I'd see if someone might be able to be of assistance.

Here is what I'd like to do...

I am presenting stimuli in the form of 3 digits with varying numbers
of spacing between them. The digit spans will be increasing in length
up to the equivalent of 12 letters long. For example. 1 2 3 (3
numbers with a total of 3 spaces, equivalent to a word 6 letters
long) or 1 2 3 (3 numbers with a total of 9 spaces, equivalent
to a word 12 letters long).

For each trial I would like to randomly generate the 3 digits ranging
from 101 - 989, without having the same digit in two adjacent
positions (e.g., 331). I would like to be able to specify the number
of spaces between each digit (so I can control the length of the
"digit" word).

I guess I would like to randomly select a number 1-9 for position A, a
random number for position B, 0-9, not equal to position A, and a
random number, 0-9 for position C, not equal to position B. I would
like to store this number somewhere and specify the number of spaces
between positions A, B, and C, and then have this number displayed on
the screen. Is this doable?

I am looking to have 30 items per trial.

I would also like to be able to have the "digit" words outputted to a
file so that I can cross reference them.

My client is giving a verbal response to the items seen on screen.

Any help would be greatly appreciated! If I'm asking too much I
apologize, and maybe you can point to some good resources...

Thanks in advance,
Bailey

Emily Ward

unread,
Feb 9, 2011, 8:51:54 PM2/9/11
to psychop...@googlegroups.com
Hi Bailey,

Your experiment sounds straight-forward and easily programmable in
python. Because of this, I recommend that you use this as motivation
to start to learn the basics of python. While I could whip up a quick
script to give you the answer, I think that you're better off in the
long run putting in the effort, struggling through it, and eventually
learning enough to prevent these types of trial structures from being
an issue again.

The best resource for python is google. Searching for "python
[something with which I'm having trouble]" is the first step for me.
In every single case so far, someone else has had a similar problem
and I've been able to work with their solutions to answer my question.

That being said, here are some places to start:

range(10) : gives you a list of the numbers 0-10

random.choice([item1,item2,...itemN]) : gives you a randomly chosen
item in the list
(remember to type "import random" before calling random)

4 != 5 : python's "not equal" operator.

Lastly, I like to store trial information in lists of dictionaries.
This looks like:

trialList = [{'trial':1, 'spaces':1, 'firstNumber': 2, 'secondNumber':
4,'thirdNumber':1},
{'trial':2, 'spaces':2, 'firstNumber': 4, 'secondNumber': 6,'thirdNumber':7}]
...etc ]

You can add to this by typing:

trialList.append({'trial':3, 'spaces':3, 'firstNumber': 5,
'secondNumber': 1,'thirdNumber':8})

and access each individual variable like this:

for currentTrial in trialList:
print 'number of spaces:',currentTrial['spaces']
print currentTrial['firstNumber'],currentTrial['secondNumber'],currentTrial['thirdNumber']


For which you'll get:

number of spaces: 1
2 4 1
number of spaces: 2
4 6 7
number of spaces: 3
5 1 8


I hope this gives you a good start with your scripting. Good luck!

Emily

> --
> You received this message because you are subscribed to the Google Groups "psychopy-users" group.
> To post to this group, send email to psychop...@googlegroups.com.
> To unsubscribe from this group, send email to psychopy-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/psychopy-users?hl=en.
>
>

Samuel Messing

unread,
Feb 9, 2011, 8:57:32 PM2/9/11
to psychop...@googlegroups.com
Hey Bailey,

Just to add to Emily's post (Hi Emily!), I'd suggest you check out Dive Into Python:


It's a great resource for getting started. Everything you need to know for your script can be found in chapters 1 -3(ish).

Also, a nice tip for printing: Say you have a variable "spaces" which is an int, specifying the number of spaces you want between each of your three digits, an easy and concise way to put int the adequate number of spaces would be:

print str(digit_1) + (" " * spaces) + str(digit_2) + (" " * spaces) + str(digit_3)

Here I assume digit_1, digit_2 and digit_3 are ints. As such, they need to be converted to strings if you want to concatenate them to strings (otherwise you get a TypeError).

S

On Wed, Feb 9, 2011 at 7:29 PM, BaileyMan <bailey...@gmail.com> wrote:

Gary Lupyan

unread,
Feb 9, 2011, 9:03:03 PM2/9/11
to psychop...@googlegroups.com
Hey Sam :)

An alternative is
print str(" "*spaces).join([digit_1,digit_2,digit_3])
or more likely, just
print str(" "*spaces).join(digits)

-----------------------------------------------------
Gary Lupyan - lup...@wisc.edu
Assistant Professor of Psychology
University of Wisconsin, Madison
http://mywebspace.wisc.edu/lupyan/web/
-----------------------------------------------------

BaileyMan

unread,
Feb 10, 2011, 9:30:54 AM2/10/11
to psychopy-users
Thanks everyone for the info, the links, the useful commands, etc.! I
look forward to playing around...

Regarding the spaces, correct me if I'm wrong...Sam's suggestion would
allow me to insert variable number of spaces between each digit, while
Gary's would allow me to put the same number of spaces between each
digit?

I'll update this thread when I have made some progress...

Cheers,
Bailey

On Feb 9, 6:03 pm, Gary Lupyan <glup...@gmail.com> wrote:
> Hey Sam :)
>
> An alternative is
> print str(" "*spaces).join([digit_1,digit_2,digit_3])
> or more likely, just
> print str(" "*spaces).join(digits)
>
> -----------------------------------------------------
> Gary Lupyan - lup...@wisc.edu
> Assistant Professor of Psychology
> University of Wisconsin, Madisonhttp://mywebspace.wisc.edu/lupyan/web/

Samuel Messing

unread,
Feb 10, 2011, 9:39:57 AM2/10/11
to psychop...@googlegroups.com
Bailey,

That is correct, you'd just need to use different variables in my construction. (Gary's is so nice and compact though, so if you want to use even spaces, I definitely recommend his).

S

BaileyMan

unread,
Mar 8, 2011, 2:23:45 PM3/8/11
to psychopy-users
I haven't forgotten about this thread and about y'all's assistance.
I've been struggling to learn even the basics in Python. I'm a
graduate student in speech and language pathology. Having a need to
write code to perform psychometric / neuropsych / etc. experiments is
less than minimal for me. That's part of the reason why it's been
slow going, but I do have a client that I would like to use psychopy
with...

I do feel like I made a little more progress yesterday, but am having
a real hard time with the syntax. It's very different from my
previous very minimal experience writing scripts. I've checked out
various tutorials, but still not sure about so much of it. Like how
do I write a loop? I think Python refers to it as a while statement.
How would you compare the value in digit1 to the value in digit2 and
tell it to grab another value for digit2 if it's the same as digit1?

What I did come up with yesterday is what feels like a pretty good
road map of what I want to do, written out in more or less English.
This is what I've got:

User to input # of spaces between 1st and 2nd digits (spaces1)
User to input # of spaces between 2nd and 3rd digits (spaces2)
User to input display time (in ms) for each item (duration)

For n<=30 (where n=the number of times to run through this loop,
creating the test stimuli, i.e., Item1 – Item30)
Randomly select integer for digit1 from 1-9
Randomly select integer for digit2 from 0-9 != digit1
Randomly select integer for digit3 from 0-9 !=digit2
Itemn=digit1,spaces1,digit2,spaces2,digit3
Create list using append command consisting of Item1 – Item30
(TrialList)

Print to screen Item1 – Item30 for duration specified w/ “+” displayed
for 2 seconds between each Item
#This is the code used in psychopy to tell what to place where on
the screen
(Itemn=visual.TextStim(win=win, ori=0,
text=u'9 3 6',
pos=[0, 0], height=0.1,
color=u'white', colorSpace=u'rgb')

Create output file consisting of TrialList

Print to screen “END” when completed until “esc” key pressed


I hope this makes some sense.

I also can't figure out how to write code and save it so that I can
come back to it. Seems like it should be easy, but it's not. Also
what do y'all use to write the code in? I have python that comes
installed with Mac OS10.6, and also have IDLE, but it keeps crashing
on me. Thoughts? Suggestions?

Thanks Much!
Bailey



On Feb 10, 6:39 am, Samuel Messing <samuel.mess...@gmail.com> wrote:
> Bailey,
>
> That is correct, you'd just need to use different variables in my
> construction. (Gary's is so nice and compact though, so if you want to use
> even spaces, I definitely recommend his).
>
> S
>

Emily Ward

unread,
Mar 8, 2011, 5:50:39 PM3/8/11
to psychop...@googlegroups.com, BaileyMan
Hi again Bailey,

The best way to cover the basics is to look at the python documentation regarding flow control:

http://docs.python.org/tutorial/controlflow.html

This will give you an overview of the functions you'll need. The great thing about python is it follows very closely how you think things should be written. So your pseudocode is really close to what you'll actually use. For example:

For n<=30

      Randomly select integer for digit1 from 1-9

becomes:

for n in range(30):
   digit1 = random.choice(range(1,10))


for loops are best used when you have a pre-established number of iterations to cycle through (e.g. going through 30 trials). while loops are best used when you need to satisfy a condition, and you're not sure how long it will take (e.g. making sure two digits don't match):

while digit1==digit2:
   digit2 = random.choice(range(1,10))


It sounds like you've just been using the pre-installed python on your computer. You won't be able to run psychopy scripts from there automatically, so I'd recommend downloading the standalone version of psychopy( http://code.google.com/p/psychopy/downloads/detail?name=StandalonePsychoPy-1.63.00-OSX.10.6.dmg ) and write your code in there. You'll be able to save it easily.

After you've downloaded that, copy and paste the code from the first tutorial into the coder view editor and run it. It'll give you a sense of the necessary elements to present and experiment.


Good luck, 

Emily

BaileyMan

unread,
Mar 16, 2011, 7:24:09 PM3/16/11
to psychopy-users
Emily,

So sorry for the late reply. I was hoping to have an opportunity to
play around some more and get back to y'all, but alas my schedule has
had other things in mind for me.

I look forward to having some free time to get back to this project.
It'll feel great when I get this code written!

Thanks again for the suggestions!
Bailey

On Mar 8, 3:50 pm, Emily Ward <amyuni...@gmail.com> wrote:
> Hi again Bailey,
>
> The best way to cover the basics is to look at the python documentation
> regarding flow control:
>
> http://docs.python.org/tutorial/controlflow.html
>
> This will give you an overview of the functions you'll need. The great thing
> about python is it follows very closely how you think things should be
> written. So your pseudocode is really close to what you'll actually use. For
> example:
>
> For n<=30
>       Randomly select integer for digit1 from 1-9
>
> becomes:
>
> for n in range(30):
>    digit1 = random.choice(range(1,10))
>
> *for* loops are best used when you have a pre-established number of
> iterations to cycle through (e.g. going through 30 trials). *while* loops
> are best used when you need to satisfy a condition, and you're not sure how
> long it will take (e.g. making sure two digits don't match):
>
> while digit1==digit2:
>    digit2 = random.choice(range(1,10))
>
> It sounds like you've just been using the pre-installed python on your
> computer. You won't be able to run psychopy scripts from there
> automatically, so I'd recommend downloading the standalone version of
> psychopy(http://code.google.com/p/psychopy/downloads/detail?name=StandalonePsy...)

BaileyMan

unread,
Apr 13, 2011, 12:56:42 PM4/13/11
to psychopy-users
Well I've been away from this project as other aspects of school have
taken over my life.

I have tried to spend some more time with psychopy and python trying
to learn the language, but continue to have no luck. I think I'm
throwing in the towel on trying to figure this out on my own. Even
with the help y'all have graciously provided, and the documentation on
psychopy and python, I'm finding that I don't have enough background
in programming to put it all together and fully understand how to
expand upon the tutorials for my needs.

I think I'll have to ask around and see if I have any friends that
know python that could write some code for me...

Thanks again for all the help and the patience during the many days/
weeks between my posts.

Best,
Bailey
Reply all
Reply to author
Forward
0 new messages