[docs] [issue33114] random.sample() behavior is unexpected/unclear from docs

Scott Eilerman report at bugs.python.org
Wed Mar 21 10:52:57 EDT 2018

New submission from Scott Eilerman <scott.j.eilerman at 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):

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):
        seq1 = random.sample(choices, k)
        seq2 = random.sample(choices, k+1)
        if seq1 != seq2[:-1]:
            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 at python
components: Documentation, Library (Lib)
messages: 314201
nosy: Scott Eilerman, docs at python
priority: normal
severity: normal
status: open
title: random.sample() behavior is unexpected/unclear from docs
type: behavior
versions: Python 3.6

Python tracker <report at bugs.python.org>

More information about the docs mailing list