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 a sample selection method to random.py #37377
Comments
random.randset(n, k) returns a k length list of unique Improves on a Cookbook submission by using the I want to add this to the library because it is a simple, If approved, will add docs and a news item. |
Logged In: YES Added full patch with news item and docs. |
Logged In: YES Renamed to random.sample(n,k) to show that it is used |
1 similar comment
Logged In: YES Renamed to random.sample(n,k) to show that it is used |
Logged In: YES Added new version with local variable optimization and |
Logged In: YES Martin, do you have time to give this patch a second |
Logged In: YES Can you explain why this needs to be in the standard |
Logged In: YES Like shuffle() and choose(), random sampling without Acceptance testing often requires a fixed number of non- Some simulations also need groups of non-overlapping While sampling with replacement is trivial to implement |
Logged In: YES Thanks for the explanation. On to the implementation: How The documentation should mention the random optional argument. |
Logged In: YES I agree this is useful, but would rather see Python grow Note that I posted a module to Python-Dev not long ago that Note that when k > n/2, "the usual trick" isn't to shuffle a list, |
Logged In: YES Thanks for the quick follow-ups. The switchover ratio of six came from counting pointers As requested, I'll add the random argument to the Originally, I was going to have sample() select from an I avoided the n/2 complement selection technique I don't know why CombGen.py never made it to |
Logged In: YES Assigned to GvR for pronouncement on The current form returns a list of integers that can be used I think the function belongs in the random module since it |
Logged In: YES I'm not even looking at this, I'm delegating this to Tim. He |
Logged In: YES I'm re-learning to hate the patch process. This was a What is the next step -- Take it as is, convert the n |
Logged In: YES Well, I agree that the patch is correct in the sense of I could be convinced if you find a user of this function (or I have the feeling that anybody who would use such a So I would propose that you withdraw the patch. |
Logged In: YES Well, you're in murky waters because it's a "new feature" Python went for years without random.shuffle(), and that In contrast, people asking how to get a random k- Some of the design decisions here remain unclear. Where People who intend to use the result as a random starting CombGen never went into the std library because I never Since that's the std I hold myself to, it's also the std I'm In the absence of being able to point to potential users from |
Logged In: YES I use the routine for transaction testing in audit work. The random order is useful so that subslices of the result The cookbook poster also needed the routine in his work |
Logged In: YES Still, the question remains, why are all these functions so |
Logged In: YES FWIW, I did try out the complement selection method for If everyone prefers sample(sequence, k) to sample(n,k), I If Tim wants to send me some code to study, that's cool. I |
Logged In: YES Tim's code is at http://mail.python.org/pipermail/python-dev/2002-August/028399.html If you really need the selection in random order, wouldn't I do find returing a list of indices less intuitive than a |
Logged In: YES Guido, you may recall that you used combgen in the combgen resolved the range(n) vs sequence "dilemma" by About random vs sorted, Raymond provided a plausible It's always a puzzle figuring out where things belong. Ideally, Python would have a package for combinatorial So that's a mild dilemma: if we put one of these in, a small As to how straightforward even this is, I expect this is the |
Logged In: YES As requested, revised patch to accept a population Now that xrange() is fixed (a separate issue), this patch |
Logged In: YES P.S. The code continues to use the index list internally. By not using the population elements as dictionary keys, |
Logged In: YES I'd rather you went back to the original scheme -- as If random order is a promise of this method, than that must The return type isn't documented and should be, esp. if you Comments about use case subtleties, and algorithm You surely don't want to hear this next one <wink>, but the |
Logged In: YES Done. Added revised patches for sample(population,k) FYI, to interpret the generator test, the expected standard |
Logged In: YES Neatened-up the patch for random.sample(population,k). I think this one is ready for prime time and would be a |
Logged In: YES But I thought Tim recommends sample(n, k)? |
Logged In: YES Tim, which do you prefer? Rand6.diff is on the lauch pad, ready to go. Still, rand5.diff is also ready to go. It documents how to |
Logged In: YES Can I commit rand6.diff and be done with this one? At one time, sample(n,k) looked better because the code As of rand6.diff, sample(population,k) is equally fast and It's now faster and easier to express sample(n,k) in terms |
Logged In: YES Tim, are you still hesitant? I think this is fine. |
Logged In: YES Sorry, I have a lot on my plate, and this one overshot its |
Logged In: YES Accepted, and back to Raymond <whew!>. rand6,diff it is. Comments: + I doubt the xrange trick will work in Python 3, but time + It's awfully obscure why "return pool[-k:]" can't do a + Vertical space isn't at a premium here -- no need to + The test code will never be run (nobody runs + It looks to be a pleasant new facility. Good job! |
Logged In: YES Committed as random.py 1.36 and librandom.tex 1.31. Thanks Tim, Martin, and GvR for the reviews. Tim' comments: + If xrange disappears in Py3, objects defining __getitem__ + When k is zero, the block with "return pool[:k]" is never + Expanded single line ifs into multi-line + The tests in random.py get run only when we are |
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: