Author Scott Eilerman
Recipients Scott Eilerman, docs@python
Date 2018-03-21.14:52:57
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1521643977.18.0.467229070634.issue33114@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2018-03-21 14:52:57Scott Eilermansetrecipients: + Scott Eilerman, docs@python
2018-03-21 14:52:57Scott Eilermansetmessageid: <1521643977.18.0.467229070634.issue33114@psf.upfronthosting.co.za>
2018-03-21 14:52:57Scott Eilermanlinkissue33114 messages
2018-03-21 14:52:57Scott Eilermancreate