difference between random module in python 2.6 and 3.2?

Matej Cepl mcepl at redhat.com
Mon Feb 6 02:27:38 CET 2012


Hi,

I have this working function:

    def as_xml(self):
         out = etree.Element("or")
         for k in sorted(self.keys()):
             out.append(etree.Element("hostname",
                 attrib={'op': '=', 'value': random.choice(self[k])}))

	# ... return somehow string representing XML

and this unit test

    def test_XML_print(self):
         random.seed(1)
         expected = ... # expected XML
         observed = self.data.as_xml()
         self.assertEqual(observed, expected,
                          "Verbose print (including PCI IDs)")

Strange thing is that this unit tests correctly with python3, but fails 
with python2. The problem is that apparently python3 random.choice picks 
different element of self[k] than the one python2 (at least, both of 
them are constant in their choice).

Is it known that there is this difference? Is there a way how to make 
both random.choice select the same?

Best,

Matěj



More information about the Python-list mailing list