Hi!

Recently I encountered a situation when I needed to pick a random
population sample but with specific distribution function - paretovariate in
this particular case. After poking around in the random module I found out
that none of the so-called "sequence methods" methods support custom random
function except for random.shuffle
<https://github.com/python/cpython/blob/master/Lib/random.py#L293>.

I would like to propose a new optional keyword argument for all these sequence
methods (choice, sample, choices) that will - similarly to how shuffle does
- take a zero-argument random distribution function of the users choice.
Because it should be a zero-argument function, it would most likely require
to pass "configured" distribution function with lambda. I would see it
being used like this:

```python
import random
random.choice([1, 2, 3], random=lambda: random.paretovariate(1.75))
```

This seems reasonable to me and I would find it beneficial for end-user as
well with the flexibility it gives. In my opinion, it would also make the
sequence methods api more uniform by all of them supporting this feature (I
don't know if that's a valid argument since the shuffle supported that from
the beginning and was added about 20 years ago
<https://github.com/python/cpython/commit/6c395ba31609eeffce2428280cc5d95e4fb8058a>
:P). And since it's going to be optional kwarg, hence backward
compatibility will be preserved.

If that makes sense I'm willing to make a patch. I looked into random.py
and it looks that implementation should be easy enough. One thing I'm
concern about is how it would all relate to _randommodule.c (I have little
knowledge about C part)

Thanks!
Patryk
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/Q7KCT5RXY4PZCUDE7T66KDTS3PIJCXF6/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to