random module gives same results across all configurations?

A

Amir Michail

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
 
C

Chris Rebert

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
 
A

Amir Michail

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
 
C

Chris Rebert

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
 
C

Carl Banks

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
 
D

Diez B. Roggisch

Amir said:
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
 
A

Amir Michail

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
 
D

Diez B. Roggisch

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
 
C

Carl Banks

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top