Issue 18414: random.choices(seq, k) - Python tracker (original) (raw)

Created on 2013-07-09 13:11 by christian.heimes, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
random_choices.patch christian.heimes,2013-07-09 13:11 review
Messages (4)
msg192747 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2013-07-09 13:11
The random module has no method to obvious way to select k elements from a sequence without enforcing unique values in the result set. It's a rather useful feature for salts, randomly generated passwords and other applications. See #18405 The patch implements random.choices(seq, k) -> list
msg192781 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-07-09 23:02
How about [random.choice(seq) for i in range(k)]? Isn't it obvious enough?
msg192847 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2013-07-11 07:21
I concur with Antoine. A list comprehension is the standard, obvious idiom for making a list from repeated function calls. Putting sampling without replacement in its own method makes the operation more opaque than a plain list comprehension (which doesn't need documentation to be clear about how many calls are made, what the output type is, its relationship to random.choice, whether k can be larger than the population, etc).
msg192860 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2013-07-11 10:23
You have convinced me. It's not worth yet another API method.
History
Date User Action Args
2022-04-11 14:57:47 admin set github: 62614
2013-08-31 09:24:20 mark.dickinson link issue18888 superseder
2013-07-11 10:23:14 christian.heimes set status: open -> closedresolution: rejectedmessages: + stage: patch review -> resolved
2013-07-11 07:21:28 rhettinger set messages: +
2013-07-09 23:02:29 pitrou set nosy: + pitroumessages: +
2013-07-09 22:05:15 rhettinger set assignee: rhettinger
2013-07-09 13:11:25 christian.heimes create