New submission from Scott Eilerman <scott.j.eiler...@gmail.com>: I ran into a "bug" when using random.sample() in which I got some results I didn't expect. After digging a little more, this is either a side effect of the optimization that's made when k > 5, or I am using the function in a way that wasn't intended. If that's the case, I would recommend calling out this behavior in the documentation.
The crux of the issue is that, for a given seed, random.sample(choices,k) gives the same sequence of results for k=1 to k=5, but that sequence can be different (for the same seed) at k=6 and higher. From my initial testing this seems to only occur when 'choices' has an even length. Example code to reproduce this issue: import random seed = 199 choices = range(-10,12) for k in range(10): random.seed(seed) print(random.sample(choices,k)) Example code to look at many different occurrences of this issue: import random choices = range(-10,12) count = 0 for seed in range(200): for k in range(8): random.seed(seed) seq1 = random.sample(choices, k) random.seed(seed) seq2 = random.sample(choices, k+1) if seq1 != seq2[:-1]: print(seed) print(seq1) print(seq2) count += 1 print(f'Number of bugged results: {count}/200') To illustrate the odd/even issue, changing choices to range(-10,11) results in zero bugged results. ---------- assignee: docs@python components: Documentation, Library (Lib) messages: 314201 nosy: Scott Eilerman, docs@python priority: normal severity: normal status: open title: random.sample() behavior is unexpected/unclear from docs type: behavior versions: Python 3.6 _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue33114> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com