You are here

Generating random number using numeric.random

4 posts / 0 new
Last post
Generating random number using numeric.random
#1

Hello,

I'm developing a new protocol using PyRosetta. I hoped to use Rosetta's built-in random functions (numeric::random::gaussian for example) but couldn't find them in PyRosetta under numeric (numeric.random does not exist)
Is there a way to access them?
I remember reading that having a good random generator for stochastic highly-parallelized application is quite important. Does it make sense to use Python's built-in random class in case it did not work with Rosetta's?

Thank you!

Category: 
Post Situation: 
Thu, 2014-10-23 09:25
resiros

You may need to explicitly import the rosetta.numeric.random module. The following works for me:

import rosetta
rosetta.init()
import rosetta.numeric.random
rosetta.numeric.random.gaussian()
0.030388865984531391
rosetta.numeric.random.gaussian()
-1.4610886425562437

If that doesn't work for you, what version of PyRosetta are you using?

Both Rosetta and Python use the Mersenne Twister as a PRNG, so the quality of the random numbers should be comparable. The one concern you may have about things is that using multiple different random number generators in parallel is apparently sub-optimal. (See "Don't Trust Parallel Monte Carlo!" by Peter Hellekalek for details.) How big an issue likely depends on how integrated the decision path using the multiple random number generators are. (Light usage of the PyRosetta PRNG is probably not a substantial issue.)

Thu, 2014-10-23 13:34
rmoretti

Thank you explication and the fix! That does indeed work.

If I may ask, why is an explicit import needed.
I thought doing so:

import rosetta as r
r.numeric.random.gaussian()

(which returns AttributeError: 'module' object has no attribute 'random') would be equivalent to an explicit import.

Yours

Fri, 2014-10-24 02:47
resiros

It's actually a general feature of Python modules. Nested modules are not automatically loaded when a parent module is loaded. There are some modules which do load their sub-modules (for example, "import os" typically also loads os.path), but that's generally not the case. If you need the sub-module, you need to load it specifically.

Same thing with Rosetta modules. Some of the submodules are automatically loaded when you import just rosetta, but others (especially the more rarely used ones) have to be manually imported before their contents can be used. So "import rosetta" does the equivalent of "import rosetta.numeric" but it doesn't do "import rosetta.numeric.random" (note that once imported, the module is available through all aliases, so "import rosetta as r; import rosetta.numeric.random; r.numeric.random.gaussian()" should work.)

You should be able to see which modules from PyRosetta are currently loaded with the command:

print '\n'.join([ m for m in sorted(sys.modules.keys()) if m.startswith("rosetta") ])

Wed, 2014-10-29 10:00
rmoretti