Member Site › Forums › PyRosetta › PyRosetta – General › Generating random number using numeric.random
- This topic has 3 replies, 2 voices, and was last updated 9 years, 10 months ago by
Anonymous.
-
AuthorPosts
-
-
October 23, 2014 at 4:25 pm #2043
Anonymous
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!
-
October 23, 2014 at 8:34 pm #10479
Anonymous
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.4610886425562437If 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.)
-
October 24, 2014 at 9:47 am #10483
Anonymous
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
-
October 29, 2014 at 5:00 pm #10496
Anonymous
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”) ])
-
-
AuthorPosts
- You must be logged in to reply to this topic.