Sep-16-2022, 06:11 PM
(This post was last modified: Sep-16-2022, 06:11 PM by deanhystad.)
Read the documentation. I think the docs.python.org documentation is very good.
https://docs.python.org/3/library/random.html
Once an element is selected by random.sample() it cannot be selected again. random.choices() can select the same element multiple times. A side effect of this is that random.choices() can return a list that is larger than population (k > len(population)), but random.sample() will raise an exception.
https://docs.python.org/3/library/random.html
Quote:random.choices(population, weights=None, *, cum_weights=None, k=1)
Return a k sized list of elements chosen from the population with replacement. If the population is empty, raises IndexError.
Quote:random.sample(population, k, *, counts=None)
Return a k length list of unique elements chosen from the population sequence or set. Used for random sampling without replacement.
Once an element is selected by random.sample() it cannot be selected again. random.choices() can select the same element multiple times. A side effect of this is that random.choices() can return a list that is larger than population (k > len(population)), but random.sample() will raise an exception.