is random.random() os/cpu independent ?



  • 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,



  • 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



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



  • 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



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



  • 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()
    


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

    Ah got it! Thanks :)



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

    hey,

    thanks for your answers.


Log in to reply