Issue 18900: Add the random.distrib module (original) (raw)

In some functions in the random module checking input arguments and precomputation takes a considerable portion of time. Here is a sample implementation of new random.distrib module which provides alternative faster interface to generating of random distributed values. It contains generators which generates values with same distributions as functions with same name in the random module.

Benchmark results:

                            random distrib

random() 0.061 0.055 1.12 randrange(0, 100, 5) 1.494 0.620 2.41 randint(1, 100) 1.283 0.551 2.33 uniform(-10.0, 10.0) 0.332 0.121 2.73 triangular(0.0, 10.0, 6.0) 0.661 0.317 2.09 gauss(5.0, 2.0) 0.707 0.280 2.53 normalvariate(5.0, 2.0) 0.867 0.553 1.57 lognormvariate(5.0, 2.0) 1.078 0.640 1.68 expovariate(0.1,) 0.508 0.293 1.73 vonmisesvariate(1.0, 1.0) 1.201 0.671 1.79 gammavariate(0.35, 1.45) 1.117 0.508 2.20 betavariate(2.71828, 3.14159) 2.868 1.776 1.61 paretovariate(5.0,) 0.493 0.238 2.07 weibullvariate(1.0, 3.0) 0.670 0.402 1.67 choice([0, 1, 2, 3, 4, 5, 6... 0.887 0.594 1.49

Distrib functions are 1.5-2.8 times faster than random functions. Weighted choice() function (see ) can be even dozens times faster (depends on size of the input).

In additional some random generators (i.e. gauss()) looks simpler when implemented as generators. distrib.gauss() is twice faster than distrib.normalvariate() (both generates numbers with same distribution) and I think some other generators can be implemented more efficient in generator style.

Of course if this idea will be accepted we can turn current functions in the random module into wrappers around generators from the distrib module.

E.g.:

def triangular(self, *args, **kwargs):
    return next(triangular(*args, random=self, **kwargs))