Different Random Numbers Between Already/Not Yet Compiled Code

Eric Eide eeide at cs.utah.edu
Thu Jan 24 22:20:12 CET 2002

"Eric" == Eric Eide <eeide at cs.utah.edu> and
"Tim" == Tim Peters <tim.one at home.com> write:

	Eric> When my simulation program iterated over the list returned by
	Eric> `items', it applied a predictable series of random numbers to an
	Eric> unpredictable arrangement of my objects, thus yielding
	Eric> unpredictable results.

	Tim> I'd sure like to see a concrete example.  The only semi-plausible
	Tim> cause I can think of is if your keys are instances of a class that
	Tim> doesn't define __hash__; then they're hashed by raw memory
	Tim> addresses, and those can vary for all sorts of reasons -- even
	Tim> *across* runs of your program, all from .pyc or all from .py.

Yes, my keys are as you describe: instances of a class that doesn't define
`__hash__'.  Being a novice Pythonist, I didn't realize that this was a bad
thing.  My program is careful not to duplicate the instances used as keys, and
lookups based on `is'-ness seem to be working.

Still, I guess I get no points for style :-).  I'll fix this in my program.

	Eric> Sorting the returned `items' list before iterating over it solved
	Eric> my unpredictability problem,

	Tim> A good solution -- unless you're merely sorting by the keys'
	Tim> memory addresses (and then this problem will come up again, sooner
	Tim> or later).

No, at least I knew not to do that :-).

Thanks again ---


Eric Eide <eeide at cs.utah.edu>  .         University of Utah School of Computing
http://www.cs.utah.edu/~eeide/ . +1 (801) 585-5512 voice, +1 (801) 581-5843 FAX

More information about the Python-list mailing list