psychopy.org | Reference | Downloads | Github

Does the random function work differently on PsychoPy coder and code component for Builder view?


#1

In the code component of the builder, using the code below works fine.

nEls = 1000
xys = random([nEls,2])*400-200 #400 pix centered on zero

However in the coder of PsychoPy, if I were to use the following code:

 import random 
 nEls = 1000
 xys = random([nEls,2])*400-200 #400 pix centered on zero

The following error appears:
TypeError: 'module' object is not callable

And if I were to include random.random as follow:

 import random 
 nEls = 1000
 xys = random.random([nEls,2])*400-200 #400 pix centered on zero

The resulting error is:
TypeError: random() takes no arguments (1 given)

Am I making an error in the coder of PsychoPy? Is it not possible to use the random function in the coder?

Side note: the following works just fine, but it only generates 1 value

import random
xys = random.random()*400-200 #400 pix centered on zero

Thank you.


#2

If you compile a script from Builder you can see how it does the importing I’d various functions like random. We import random from the numpy library (numeric python) which supports creating arrays.


#3

Thank you for that!

Using the following works:
from numpy.random import random


#4

This error statement TypeError: ‘module’ object is not callable is raised as you are being confused about the Class name and Module name. The problem is in the import line . You are importing a module, not a class. This happend because the module name and class name have the same name .

If you have a class “MyClass” in a file called “MyClass.py” , then you should import :

from MyClass import MyClass

In Python , a script is a module, whose name is determined by the filename . So when you start out your file MyClass.py with import MyClass you are creating a loop in the module structure.