(self, population, k, *, counts=None)
return result
Chooses k unique random elements from a population sequence or set.

        Returns a new list containing elements from the population while
        leaving the original population unchanged.  The resulting list is
        in selection order so that all sub-slices will also be valid random
        samples.  This allows raffle winners (the sample) to be partitioned
        into grand prize and second place winners (the subslices).

        Members of the population need not be hashable or unique.  If the
        population contains repeats, then each occurrence is a possible
        selection in the sample.

        Repeated elements can be specified one at a time or with the optional
        counts parameter.  For example:

            sample(['red', 'blue'], counts=[4, 2], k=5)

        is equivalent to:

            sample(['red', 'red', 'red', 'red', 'blue', 'blue'], k=5)

        To choose a sample from a range of integers, use range() for the
        population argument.  This is especially fast and space efficient
        for sampling from a large population:

            sample(range(10000000), 60)
Chooses k unique random elements from a population sequence or set.
Chooses k unique random elements from a population sequence or set .
def sample(self, population, k, *, counts=None):
        """Chooses k unique random elements from a population sequence or set.

        Returns a new list containing elements from the population while
        leaving the original population unchanged.  The resulting list is
        in selection order so that all sub-slices will also be valid random
        samples.  This allows raffle winners (the sample) to be partitioned
        into grand prize and second place winners (the subslices).

        Members of the population need not be hashable or unique.  If the
        population contains repeats, then each occurrence is a possible
        selection in the sample.

        Repeated elements can be specified one at a time or with the optional
        counts parameter.  For example:

            sample(['red', 'blue'], counts=[4, 2], k=5)

        is equivalent to:

            sample(['red', 'red', 'red', 'red', 'blue', 'blue'], k=5)

        To choose a sample from a range of integers, use range() for the
        population argument.  This is especially fast and space efficient
        for sampling from a large population:

            sample(range(10000000), 60)

        """

        # Sampling without replacement entails tracking either potential
        # selections (the pool) in a list or previous selections in a set.

        # When the number of selections is small compared to the
        # population, then tracking selections is efficient, requiring
        # only a small set and an occasional reselection.  For
        # a larger number of selections, the pool tracking method is
        # preferred since the list takes less space than the
        # set and it doesn't suffer from frequent reselections.

        # The number of calls to _randbelow() is kept at or near k, the
        # theoretical minimum.  This is important because running time
        # is dominated by _randbelow() and because it extracts the
        # least entropy from the underlying random number generators.

        # Memory requirements are kept to the smaller of a k-length
        # set or an n-length list.

        # There are other sampling algorithms that do not require
        # auxiliary memory, but they were rejected because they made
        # too many calls to _randbelow(), making them slower and
        # causing them to eat more entropy than necessary.

        if not isinstance(population, _Sequence):
            if isinstance(population, _Set):
                _warn('Sampling from a set deprecated\n'
                      'since Python 3.9 and will be removed in a subsequent version.',
                      DeprecationWarning, 2)
                population = tuple(population)
            else:
                raise TypeError("Population must be a sequence.  For dicts or sets, use sorted(d).")
        n = len(population)
        if counts is not None:
            cum_counts = list(_accumulate(counts))
            if len(cum_counts) != n:
                raise ValueError('The number of counts does not match the population')
            total = cum_counts.pop()
            if not isinstance(total, int):
                raise TypeError('Counts must be integers')
            if total <= 0:
                raise ValueError('Total of counts must be greater than zero')
            selections = self.sample(range(total), k=k)
            bisect = _bisect
            return [population[bisect(cum_counts, s)] for s in selections]
        randbelow = self._randbelow
        if not 0 <= k <= n:
            raise ValueError("Sample larger than population or is negative")
        result = [None] * k
        setsize = 21        # size of a small set minus size of an empty list
        if k > 5:
            setsize += 4 ** _ceil(_log(k * 3, 4))  # table size for big sets
        if n <= setsize:
            # An n-length list is smaller than a k-length set.
            # Invariant:  non-selected at pool[0 : n-i]
            pool = list(population)
            for i in range(k):
                j = randbelow(n - i)
                result[i] = pool[j]
                pool[j] = pool[n - i - 1]  # move non-selected item into vacancy
        else:
            selected = set()
            selected_add = selected.add
            for i in range(k):
                j = randbelow(n)
                while j in selected:
                    j = randbelow(n)
                selected_add(j)
                result[i] = population[j]
        return result
def sample ( self , population , k , * , counts = None ) : # Sampling without replacement entails tracking either potential # selections (the pool) in a list or previous selections in a set. # When the number of selections is small compared to the # population, then tracking selections is efficient, requiring # only a small set and an occasional reselection.  For # a larger number of selections, the pool tracking method is # preferred since the list takes less space than the # set and it doesn't suffer from frequent reselections. # The number of calls to _randbelow() is kept at or near k, the # theoretical minimum.  This is important because running time # is dominated by _randbelow() and because it extracts the # least entropy from the underlying random number generators. # Memory requirements are kept to the smaller of a k-length # set or an n-length list. # There are other sampling algorithms that do not require # auxiliary memory, but they were rejected because they made # too many calls to _randbelow(), making them slower and # causing them to eat more entropy than necessary. if not isinstance ( population , _Sequence ) : if isinstance ( population , _Set ) : _warn ( 'Sampling from a set deprecated\n' 'since Python 3.9 and will be removed in a subsequent version.' , DeprecationWarning , 2 ) population = tuple ( population ) else : raise TypeError ( "Population must be a sequence.  For dicts or sets, use sorted(d)." ) n = len ( population ) if counts is not None : cum_counts = list ( _accumulate ( counts ) ) if len ( cum_counts ) != n : raise ValueError ( 'The number of counts does not match the population' ) total = cum_counts . pop ( ) if not isinstance ( total , int ) : raise TypeError ( 'Counts must be integers' ) if total <= 0 : raise ValueError ( 'Total of counts must be greater than zero' ) selections = self . sample ( range ( total ) , k = k ) bisect = _bisect return [ population [ bisect ( cum_counts , s ) ] for s in selections ] randbelow = self . _randbelow if not 0 <= k <= n : raise ValueError ( "Sample larger than population or is negative" ) result = [ None ] * k setsize = 21 # size of a small set minus size of an empty list if k > 5 : setsize += 4 ** _ceil ( _log ( k * 3 , 4 ) ) # table size for big sets if n <= setsize : # An n-length list is smaller than a k-length set. # Invariant:  non-selected at pool[0 : n-i] pool = list ( population ) for i in range ( k ) : j = randbelow ( n - i ) result [ i ] = pool [ j ] pool [ j ] = pool [ n - i - 1 ] # move non-selected item into vacancy else : selected = set ( ) selected_add = selected . add for i in range ( k ) : j = randbelow ( n ) while j in selected : j = randbelow ( n ) selected_add ( j ) result [ i ] = population [ j ] return result