random module gives same results across all configurations?

Discussion in 'Python' started by Amir Michail, Mar 4, 2009.

  1. Amir  Michail

    Amir Michail Guest

    Hi,

    Is it the case that the random module will always give the same
    results if given the same seed across all configurations (e.g.,
    architectures, compilers, etc.)?

    Amir
     
    Amir Michail, Mar 4, 2009
    #1
    1. Advertising

  2. Amir  Michail

    Chris Rebert Guest

    On Tue, Mar 3, 2009 at 6:59 PM, Amir Michail <> wrote:
    > Hi,
    >
    > Is it the case that the random module will always give the same
    > results if given the same seed across all configurations (e.g.,
    > architectures, compilers, etc.)?


    Your question is vague. Define what you mean by "same results" in this context.

    Cheers,
    Chris

    --
    I have a blog:
    http://blog.rebertia.com
     
    Chris Rebert, Mar 4, 2009
    #2
    1. Advertising

  3. Amir  Michail

    Amir Michail Guest

    On Mar 3, 10:05 pm, Chris Rebert <> wrote:
    > On Tue, Mar 3, 2009 at 6:59 PM, Amir Michail <> wrote:
    > > Hi,

    >
    > > Is it the case that the random module will always give the same
    > > results if given the same seed across all configurations (e.g.,
    > > architectures, compilers, etc.)?

    >
    > Your question is vague. Define what you mean by "same results" in this context.


    In Python 2.5, if you use the same seed, will calls to randrange yield
    the same numbers across all configurations?

    Amir

    >
    > Cheers,
    > Chris
    >
    > --
    > I have a blog:http://blog.rebertia.com
     
    Amir Michail, Mar 4, 2009
    #3
  4. Amir  Michail

    Chris Rebert Guest

    On Tue, Mar 3, 2009 at 7:11 PM, Amir Michail <> wrote:
    > On Mar 3, 10:05 pm, Chris Rebert <> wrote:
    >> On Tue, Mar 3, 2009 at 6:59 PM, Amir Michail <> wrote:
    >> > Hi,

    >>
    >> > Is it the case that the random module will always give the same
    >> > results if given the same seed across all configurations (e.g.,
    >> > architectures, compilers, etc.)?

    >>
    >> Your question is vague. Define what you mean by "same results" in this context.

    >
    > In Python 2.5, if you use the same seed, will calls to randrange yield
    > the same numbers across all configurations?


    Ah, my apologies, I overlooked part of your original post. My
    understanding is that yes, that should be the case (with the possible
    exceptions of alternate Python implementations such as Jython and
    IronPython, but sounds like you're only talking about CPython).

    Cheers,
    Chris

    --
    I have a blog:
    http://blog.rebertia.com
     
    Chris Rebert, Mar 4, 2009
    #4
  5. Amir  Michail

    Carl Banks Guest

    On Mar 3, 6:59 pm, Amir Michail <> wrote:
    > Hi,
    >
    > Is it the case that the random module will always give the same
    > results if given the same seed across all configurations (e.g.,
    > architectures, compilers, etc.)?



    If you need a repeatable sequence, such as for unit testing, you can
    subclass random.Random to do it. (See the source code to random.py
    for example.)


    Carl Banks
     
    Carl Banks, Mar 4, 2009
    #5
  6. Amir Michail schrieb:
    > On Mar 3, 10:05 pm, Chris Rebert <> wrote:
    >> On Tue, Mar 3, 2009 at 6:59 PM, Amir Michail <> wrote:
    >>> Hi,
    >>> Is it the case that the random module will always give the same
    >>> results if given the same seed across all configurations (e.g.,
    >>> architectures, compilers, etc.)?

    >> Your question is vague. Define what you mean by "same results" in this context.

    >
    > In Python 2.5, if you use the same seed, will calls to randrange yield
    > the same numbers across all configurations?


    What do you mean with "configurations"?


    Random uses AFAIK rand/srand from the stdlib.h of your platform (*nix,
    no idea how that beast is called in redmond).

    So whatever the behaviour, it will be driven by that, and I guess it
    varies between platforms.

    Diez
     
    Diez B. Roggisch, Mar 4, 2009
    #6
  7. Amir  Michail

    Amir Michail Guest

    On Mar 4, 2:26 am, "Diez B. Roggisch" <> wrote:
    > Amir Michail schrieb:
    >
    > > On Mar 3, 10:05 pm, Chris Rebert <> wrote:
    > >> On Tue, Mar 3, 2009 at 6:59 PM, Amir Michail <> wrote:
    > >>> Hi,
    > >>> Is it the case that the random module will always give the same
    > >>> results if given the same seed across all configurations (e.g.,
    > >>> architectures, compilers, etc.)?
    > >> Your question is vague. Define what you mean by "same results" in this context.

    >
    > > In Python 2.5, if you use the same seed, will calls to randrange yield
    > > the same numbers across all configurations?

    >
    > What do you mean with "configurations"?
    >
    > Random uses AFAIK rand/srand from the stdlib.h of your platform (*nix,
    > no idea how that beast is called in redmond).
    >
    > So whatever the behaviour, it will be driven by that, and I guess it
    > varies between platforms.
    >
    > Diez


    So far I get the same results under Mac OS X, Windows, and Linux
    (Google App Engine). I'm particularly interested in getting the same
    results under the Google App Engine even as Google upgrades its
    servers over time.

    Amir
     
    Amir Michail, Mar 4, 2009
    #7
  8. >
    > So far I get the same results under Mac OS X, Windows, and Linux
    > (Google App Engine). I'm particularly interested in getting the same
    > results under the Google App Engine even as Google upgrades its
    > servers over time.


    I just had a look into the python source - and I was wrong, it appears
    random is implemented as part of the python c modules itself.

    So chances are good that unless this module changes, you get a stable
    behaviour.

    But I'd not advise to rely on that. Why do you want that anyway?

    Diez
     
    Diez B. Roggisch, Mar 4, 2009
    #8
  9. Amir  Michail

    Paul Rubin Guest

    "Diez B. Roggisch" <> writes:
    > Random uses AFAIK rand/srand from the stdlib.h of your platform (*nix,
    > no idea how that beast is called in redmond).


    According to http://docs.python.org/library/random.html
    it uses Mersenne Twister.
     
    Paul Rubin, Mar 4, 2009
    #9
  10. Paul Rubin schrieb:
    > "Diez B. Roggisch" <> writes:
    >> Random uses AFAIK rand/srand from the stdlib.h of your platform (*nix,
    >> no idea how that beast is called in redmond).

    >
    > According to http://docs.python.org/library/random.html
    > it uses Mersenne Twister.


    Yeah, I figured that out later, sorry for the FUD.

    Diez
     
    Diez B. Roggisch, Mar 4, 2009
    #10
  11. Amir  Michail

    Carl Banks Guest

    On Mar 4, 12:33 am, "Diez B. Roggisch" <> wrote:
    > > So far I get the same results under Mac OS X, Windows, and Linux
    > > (Google App Engine).  I'm particularly interested in getting the same
    > > results under the Google App Engine even as Google upgrades its
    > > servers over time.

    >
    > I just had a look into the python source - and I was wrong, it appears
    > random is implemented as part of the python c modules itself.


    The standard library's rand and srand functions had a reputation for
    being notoriously bad on a lot of platforms. I remember back in the
    day the advice was (and probably still is a good idea today) that if
    you needed a small random number, never mask out the high bits, but
    instead divide a random number from the full range by .

    I never actually tested rand, but if it's as bad as they said it was,
    it's a good thing Python doesn't use it.


    > So chances are good that unless this module changes, you get a stable
    > behaviour.
    >
    > But I'd not advise to rely on that. Why do you want that anyway?


    All kinds of reasons. For a unit test you might want a repeatable
    sequence, or even a canned fake-random sequence. Sometimes random
    sequences are used to generate data, and you might want to give the
    user power to regenerate the same data by seeding the PRNG with the
    same seed. (A well-known example is "Free Cell", where the game
    number is just an RNG seed.)


    Carl Banks
     
    Carl Banks, Mar 4, 2009
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UHVuaXNoZXI=?=

    Need same viewstate for same page across multiple servers

    =?Utf-8?B?UHVuaXNoZXI=?=, Mar 23, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    437
    John Timney \( MVP \)
    Mar 23, 2006
  2. Monique Y. Mudama
    Replies:
    1
    Views:
    477
    Monique Y. Mudama
    Jun 28, 2005
  3. globalrev
    Replies:
    4
    Views:
    810
    Gabriel Genellina
    Apr 20, 2008
  4. LANkrypt0

    Random letters, getting same results

    LANkrypt0, Jun 19, 2004, in forum: Perl Misc
    Replies:
    15
    Views:
    187
    John Bokma
    Jun 20, 2004
  5. VK
    Replies:
    15
    Views:
    1,313
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page