diff -r a8af49baa636 Lib/test/test_random.py --- a/Lib/test/test_random.py Sun Apr 22 10:27:22 2012 +0800 +++ b/Lib/test/test_random.py Sun Apr 22 13:56:09 2012 +0100 @@ -57,6 +57,14 @@ self.assertRaises(TypeError, self.gen.jumpahead) # needs an arg self.assertRaises(TypeError, self.gen.jumpahead, 2, 3) # too many + def test_jumpahead_produces_valid_state(self): + # From http://bugs.python.org/issue14591. + self.gen.seed(199210368) + self.gen.jumpahead(13550674232554645900) + for i in range(500): + val = self.gen.random() + self.assertTrue(val < 1.0) + def test_sample(self): # For the entire allowable range of 0 <= k <= N, validate that # the sample is of the correct length and contains only unique items diff -r a8af49baa636 Modules/_randommodule.c --- a/Modules/_randommodule.c Sun Apr 22 10:27:22 2012 +0800 +++ b/Modules/_randommodule.c Sun Apr 22 13:56:09 2012 +0100 @@ -427,8 +427,19 @@ mt[j] = tmp; } - for (i = 0; i < N; i++) + for (i = 0; i < N; i++) { mt[i] += i+1; + mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */ + } + + /* Make sure the state is nonzero: in the unlikely event that mt[1] through + mt[N-1] are all zero, we set the MSB of mt[0]. */ + tmp = 0; + for (i = 1; i < N; i++) { + tmp |= mt[i]; + } + if (tmp == 0) + mt[0] = 0x80000000UL; self->index = N; Py_INCREF(Py_None);