New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add the random.distrib module #63100
Comments
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() 0.061 0.055 1.12 Distrib functions are 1.5-2.8 times faster than random functions. Weighted choice() function (see bpo-18844) 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. |
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. |
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)) |
Makes sense. In light of Raymond's comments on code bloat in bpo-18844, 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. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: