Generating random number using numeric.random

Member Site Forums PyRosetta PyRosetta – General Generating random number using numeric.random

Viewing 1 reply thread
  • Author
    • #2043


        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!

      • #10479

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

          import rosetta
          import rosetta.numeric.random

          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.)

        • #10483

            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

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


          • #10496

              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”) ])

          Viewing 1 reply thread
          • You must be logged in to reply to this topic.