diff -r 5f8c68281d18 Lib/random.py --- a/Lib/random.py Wed Feb 06 10:37:19 2013 +0200 +++ b/Lib/random.py Wed Feb 06 14:43:04 2013 +0200 @@ -343,7 +343,12 @@ """ u = self.random() - c = 0.5 if mode is None else (mode - low) / (high - low) + if mode is None: + c = 0.5 + elif mode == high: + c = 1 + else: + c = (mode - low) / (high - low) if u > c: u = 1.0 - u c = 1.0 - c diff -r 5f8c68281d18 Lib/test/test_random.py --- a/Lib/test/test_random.py Wed Feb 06 10:37:19 2013 +0200 +++ b/Lib/test/test_random.py Wed Feb 06 14:43:04 2013 +0200 @@ -512,6 +512,25 @@ self.assertAlmostEqual(s1/N, mu, places=2) self.assertAlmostEqual(s2/(N-1), sigmasqrd, places=2) + def test_constant(self): + g = random.Random() + N = 100 + for variate, args, expected in [ + (g.uniform, (10.0, 10.0), 10.0), + (g.triangular, (10.0, 10.0), 10.0), + (g.triangular, (10.0, 10.0, 10.0), 10.0), + (g.expovariate, (float('inf'),), 0.0), + (g.gauss, (10.0, 0.0), 10.0), + (g.lognormvariate, (0.0, 0.0), 1.0), + (g.lognormvariate, (-float('inf'), 0.0), 0.0), + (g.normalvariate, (10.0, 0.0), 10.0), + (g.paretovariate, (float('inf'),), 1.0), + (g.weibullvariate, (10.0, float('inf')), 10.0), + (g.weibullvariate, (0.0, 10.0), 0.0), + ]: + for i in range(N): + self.assertEqual(variate(*args), expected) + class TestModule(unittest.TestCase): def testMagicConstants(self): self.assertAlmostEqual(random.NV_MAGICCONST, 1.71552776992141)