This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Add the random.distrib module
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.4
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: madison.may, mark.dickinson, rhettinger, serhiy.storchaka, tim.peters
Priority: normal Keywords:

Created on 2013-09-01 18:49 by serhiy.storchaka, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
distrib.py serhiy.storchaka, 2013-09-01 18:49 Sample implementation
distrib_bench.py serhiy.storchaka, 2013-09-01 18:51 Benchmark script
Messages (5)
msg196727 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-09-01 18:49
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 issue18844) 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.
msg196732 - (view) Author: Madison May (madison.may) * Date: 2013-09-01 19:58
I like the core idea of a family of random generators, but it feels like a new module that's nearly identical to random introduces a lot of repeated code.

Perhaps adding an additional optional arg ('generator=False', for example) to these functions in the random module would be a bit simpler.
msg196744 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-09-01 22:24
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))
msg196752 - (view) Author: Madison May (madison.may) * Date: 2013-09-01 23:20
> ...we can turn current functions in the random module into wrappers 
> around generators from the distrib module.

Makes sense.

In light of Raymond's comments on code bloat in issue18844, perhaps this module could be added to PyPi to see whether or not there's interest in this kind of functionality?
msg196778 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-09-02 11:12
> In light of Raymond's comments on code bloat in issue18844, perhaps this module could be added to PyPi to see whether or not there's interest in this kind of functionality?

Agree. At first look there are no module which provides such features on PyPI. On the second hand NumpPy provides efficient C-implemented functions which are 2-10 times faster than proposed pure Python iterators. Due to this fact I withdraw my proposition. Anyone who need a performance in random generation with specific distribution can use NumPy.
History
Date User Action Args
2022-04-11 14:57:50adminsetgithub: 63100
2013-09-07 07:24:09serhiy.storchakasetstatus: pending -> closed
stage: resolved
2013-09-02 11:12:08serhiy.storchakasetstatus: open -> pending
resolution: rejected
messages: + msg196778
2013-09-01 23:20:41madison.maysetmessages: + msg196752
2013-09-01 22:24:41serhiy.storchakasetmessages: + msg196744
2013-09-01 21:00:28pitrousetnosy: + tim.peters
2013-09-01 19:58:26madison.maysetnosy: + madison.may
messages: + msg196732
2013-09-01 18:51:05serhiy.storchakasetfiles: + distrib_bench.py
2013-09-01 18:49:28serhiy.storchakacreate