result[i] = pool[j]
pool[j] = pool[n-i-1] # move non-selected item into vacancy
else:
+ try:
+ n > 0 and (population[0], population[n//2], population[n-1])
+ except (TypeError, KeyError): # handle sets and dictionaries
+ population = tuple(population)
selected = {}
for i in xrange(k):
j = _int(random() * n)
else:
self.fail()
+ def test_sample_inputs(self):
+ # SF bug #801342 -- population can be any iterable defining __len__()
+ from sets import Set
+ self.gen.sample(Set(range(20)), 2)
+ self.gen.sample(range(20), 2)
+ self.gen.sample(xrange(20), 2)
+ self.gen.sample(dict.fromkeys('abcdefghijklmnopqrst'), 2)
+ self.gen.sample(str('abcdefghijklmnopqrst'), 2)
+ self.gen.sample(unicode('abcdefghijklmnopqrst'), 2)
+ self.gen.sample(tuple('abcdefghijklmnopqrst'), 2)
+
def test_gauss(self):
# Ensure that the seed() method initializes all the hidden state. In
# particular, through 2.2.1 it failed to reset a piece of state used
allow any iterable. Also the Set.update() has been deprecated because
it duplicates Set.union_update().
+- Bug #801342: random.sample() now accepts a Set as a possible argument.
+ Previously, it insisted that the population argument be indexable.
+
- Bug #778964: random.seed() now uses fractional seconds so that
rapid successive, seeding calls will produce different sequences.