[issue46190] Omit k in random.sample()

2021-12-29 Thread Tilman Krummeck
Tilman Krummeck added the comment: Hmm, ok, that sounds obvious. Thanks for the clarification. -- ___ Python tracker ___ ___

[issue46190] Omit k in random.sample()

2021-12-29 Thread Raymond Hettinger
Raymond Hettinger added the comment: The use case isn't bizarre. But having an API where that is the default behavior would be. From the point of view of most users, such an API would be unusual and surprising (I don't know of any other random package that has such a default). --

[issue46190] Omit k in random.sample()

2021-12-28 Thread Tilman Krummeck
Tilman Krummeck added the comment: Well, it's not bizarre, it's a use-case I'm facing quite often. But thanks for the clarification, I haven't had very large populations in mind - this makes indeed sense. -- ___ Python tracker

[issue46190] Omit k in random.sample()

2021-12-28 Thread Raymond Hettinger
Raymond Hettinger added the comment: > My suggestion is not to set k=1 when omitted but to assign it a random value Sorry, I think that is just bizarre. Also, some populations are *very* large, so a minor user accident of omitting a parameter would result in a large unexpected output.

[issue46190] Omit k in random.sample()

2021-12-28 Thread Tilman Krummeck
Tilman Krummeck added the comment: My suggestion is not to set k=1 when omitted but to assign it a random value that is something between 0 and the maximum possible value which is: sum(counts) if counts else len(population) -- ___ Python tracker

[issue46190] Omit k in random.sample()

2021-12-28 Thread Dennis Sweeney
Dennis Sweeney added the comment: For completeness: def random_subset_with_counts(sequence, counts): result = [] for x, k in zip(sequence, counts): result.extend([x] * random.getrandbits(k).bit_count()) return result -- ___

[issue46190] Omit k in random.sample()

2021-12-28 Thread Raymond Hettinger
Raymond Hettinger added the comment: Okay. Thank for the quick response and the suggestion. I'm going to mark this one as closed. AFAICT, it distracts users from better solutions. I did a quick code search for sample(). The k==1 case is rare and in most cases the code should have used

[issue46190] Omit k in random.sample()

2021-12-28 Thread Tilman Krummeck
Tilman Krummeck added the comment: I use this mostly in tests to randomize my inputs. So currently I'm doing something like this: result = random.sample(items, random.randint(0, len(items))) I guess if someone would omit 'k' he wouldn't care about the result (which is probably a use-case

[issue46190] Omit k in random.sample()

2021-12-28 Thread Raymond Hettinger
Raymond Hettinger added the comment: If all you want is a sample where k==1, then use choice(). That is clearer and more efficient. The sample() function is for sampling without replacement which only makes sense when k > 1; otherwise, choice() or choices() is usually what you want.

[issue46190] Omit k in random.sample()

2021-12-28 Thread Dennis Sweeney
Dennis Sweeney added the comment: Can you describe more about your use-case for this? You can already do something like this now with something like the following: def random_subset(sequence): source = random.randbytes(len(sequence)) return [x for x, r in zip(sequence,

[issue46190] Omit k in random.sample()

2021-12-28 Thread Tilman Krummeck
New submission from Tilman Krummeck : random.sample can be used to choose k items from a given sequence. Currently, k is a mandatory parameter. I suggest to make k optional and instead, if omitted, pick a random value from the range of 0 and the length of the sequence. Of course, doing