Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush Python API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Recent
    • Tags
    • Users
    • Login

    is random.random() os/cpu independent ?

    Scheduled Pinned Locked Moved PYTHON Development
    8 Posts 0 Posters 684 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H Offline
      Helper
      last edited by

      On 18/01/2013 at 00:04, xxxxxxxx wrote:

      Hi,

      can someone tell me if the python random class is OS and  CPU independent ? i tried to find
      an answer in python docs and with google, but i am still not really sure. it would be nice if 
      you could quickly run this short script.

      import random
        
      def main() :
          random.seed(0)
          for n in xrange(4) :
              print round(random.random(),2)
        
      if __name__=='__main__':
          main()
      

      it returns for me on 3 windows machines :

      0.84
      0.76
      0.42
      0.26
      

      thanks for reading,

      1 Reply Last reply Reply Quote 0
      • H Offline
        Helper
        last edited by

        On 18/01/2013 at 01:44, xxxxxxxx wrote:

        Hey.
        Getting the exact same numbers under OSX 10.6.8 and on a Windows 7 machine.
        Phil

        1 Reply Last reply Reply Quote 0
        • H Offline
          Helper
          last edited by

          On 18/01/2013 at 01:45, xxxxxxxx wrote:

          I ran this on my PC and on an iMac with the same results as yours in both cases.

          1 Reply Last reply Reply Quote 0
          • H Offline
            Helper
            last edited by

            On 18/01/2013 at 01:45, xxxxxxxx wrote:

            Hi Ferdinand,

            yes, the random module is OS and CPU independent. It is using the same algorithm on every
            machine and Python distribution (assuming CPython, I can't say this for sure for other Python
            implementations, but it should be the same). However, you should create a random.Random
            object rather than using random.seed(), because this will have influence on other Python
            plugins using the random module. (E.g. Plugins expecting "real" randomness from random.random()
            are now receiving values computed by your defined seed value, and you might skip one of the
            values because another plugin was retreiving one.)

            Best,
            Niklas

            1 Reply Last reply Reply Quote 0
            • H Offline
              Helper
              last edited by

              On 18/01/2013 at 02:15, xxxxxxxx wrote:

              I'm wondering why the same results have been generated. The docs say that the seed function uses randomness sources of the OS if provided (since 2.4). Whereby the result should differ between UNIX based platforms and Windows (see os.urandom())...

              1 Reply Last reply Reply Quote 0
              • H Offline
                Helper
                last edited by

                On 18/01/2013 at 02:25, xxxxxxxx wrote:

                Hi Satara,

                the OS randomness sources are used when the parameter passed to random.seed() is omitted
                or None. This is stated in the docs as well.

                If  x  is not None or an int or long, hash(x) is used instead. If  x  is an int or long,  x  is used directly.

                # Define your own seed value
                random.seed(x)
                  
                # Take OS randomness sources or another seed value (eg. system time)
                random.seed()
                
                1 Reply Last reply Reply Quote 0
                • H Offline
                  Helper
                  last edited by

                  On 18/01/2013 at 02:40, xxxxxxxx wrote:

                  Ah got it! Thanks 🙂

                  1 Reply Last reply Reply Quote 0
                  • H Offline
                    Helper
                    last edited by

                    On 18/01/2013 at 12:59, xxxxxxxx wrote:

                    hey,

                    thanks for your answers.

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post