473,395 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

gotcha or bug? random state reset on irrelevant import

Running test.py will print False.
Is this expected/desirable?
Thanks,
Alan Isaac
%%%%%%% test.py %%%%%%%%%%%
from random import seed, getstate
seed(217)
x = getstate()
from test2 import Trivial
y = getstate()
print x == y
%%%%% test2.py %%%%%%%%%%%%%
from random import seed
seed(314)
class Trivial:
pass

Apr 25 '07 #1
6 1626
Alan Isaac wrote:
Running test.py will print False.
Is this expected/desirable?
Thanks,
Alan Isaac
%%%%%%% test.py %%%%%%%%%%%
from random import seed, getstate
seed(217)
x = getstate()
from test2 import Trivial
y = getstate()
print x == y
%%%%% test2.py %%%%%%%%%%%%%
from random import seed
seed(314)
class Trivial:
pass
Yes. To circomvent this, use

from random import seed

class Trivial:
pass

if __name__ == "__main__":
seed(314)
Apr 25 '07 #2
On 25/04/2007 11:22 AM, Alan Isaac wrote:
Running test.py will print False.
Is this expected/desirable?
Thanks,
Alan Isaac
%%%%%%% test.py %%%%%%%%%%%
from random import seed, getstate
seed(217)
x = getstate()
from test2 import Trivial
It's not irrelevant; the test2 module has to be *executed* as part of
the import. Yes, "class" is an executable statement. Just about every
Python statement is executable. seed(314) is executed.
y = getstate()
print x == y
%%%%% test2.py %%%%%%%%%%%%%
from random import seed
Insert here:
print "before seed call"
seed(314)
Insert here:
print "have just executed some relevant code"
class Trivial:
pass
HTH,
John
Apr 25 '07 #3
"John Machin" <sj******@lexicon.netwrote in message
news:46**************@lexicon.net...
It's not irrelevant; the test2 module has to be *executed* as part of
the import. [...] seed(314) is executed.
OK, I understand this is the right answer.
But another way to look at the question is:
is it a good design to only be able to use seed
to set a global state? (See for contrast
numpy.random, for which my appreciation
has just increased yet again.)

Cheers,
Alan Isaac
Apr 25 '07 #4
"James Stroud" <js*****@mbi.ucla.eduwrote in message
news:f0**********@zinnia.noc.ucla.edu...
if __name__ == "__main__":
seed(314)
Thanks for this suggestion.
Alan Isaac
Apr 25 '07 #5
Alan Isaac <ai****@american.eduwrote:
"John Machin" <sj******@lexicon.netwrote in message
news:46**************@lexicon.net...
It's not irrelevant; the test2 module has to be *executed* as part of
the import. [...] seed(314) is executed.

OK, I understand this is the right answer.
But another way to look at the question is:
is it a good design to only be able to use seed
to set a global state? (See for contrast
numpy.random, for which my appreciation
has just increased yet again.)
What do you mean? Just instantiate the random.Random class and you can
call .seed on it as well as anything else, and no other module will
infringe on "your" module's Random instance. The "global functions" of
module random exist for those who *SPECIFICALLY* want globally shared
behavior, of course.
Alex
Apr 25 '07 #6
Alex Martelli said:
What do you mean? Just instantiate the random.Random class and you can
call .seed on it as well as anything else, and no other module will
infringe on "your" module's Random instance. The "global functions" of
module random exist for those who *SPECIFICALLY* want globally shared
behavior, of course.

I consider this an unduly kind response.
"RTFM" would have been appropriate.

The functions supplied by this module are actually bound
methods of a hidden instance of the random.Random class.
You can instantiate your own instances of Random to get generators that
don't share state.

Sheesh. I must have been asleep when reading the documentation.

Thanks,
Alan
Apr 25 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Paul Rubin | last post by:
http://www.nightsong.com/phr/python/sharandom.c This is intended to be less predicable/have fewer correlations than the default Mersenne Twister or Wichmann-Hill generators. Comments are...
8
by: Malcolm Clift | last post by:
Hi All, I have this so far. import random things = xrange(int(raw_input("choose no of things (1-8)? "))) state =
4
by: Jacob H | last post by:
Hello list... I'm developing an adventure game in Python (which of course is lots of fun). One of the features is the ability to save games and restore the saves later. I'm using the pickle...
16
by: Jason | last post by:
Hi, I need a way to use random numbers in c++. In my c++ project, when using the mingw compiler I used a mersenne twister that is publicly available and this did its job well. Now I have...
2
by: Matthew Wilson | last post by:
The random.jumpahead documentation says this: Changed in version 2.3: Instead of jumping to a specific state, n steps ahead, jumpahead(n) jumps to another state likely to be separated by many...
4
by: Kim | last post by:
Random image downloader for specified newsgroup. Hi I'm writing a small script that will download random images from a specified newsgroup. I've imported yenc into the script but I can't open the...
39
by: Alan Isaac | last post by:
This may seem very strange, but it is true. If I delete a .pyc file, my program executes with a different state! In a single directory I have module1 and module2. module1 imports random and...
4
by: George Sakkis | last post by:
I spent several hours debugging some bogus data results that turned out to be caused by the fact that heapq.nlargest doesn't respect rich comparisons: import heapq import random class...
4
n8kindt
by: n8kindt | last post by:
ok, i've done quite a bit of research on my problem here. random "! " will appear in my email that i'm sending thru cdo. i've learned that this problem occurs when there are long lines of html (or...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.