The fundamental difference is that random.choices()
will (eventually) draw elements at the same position (always sample from the entire sequence, so, once drawn, the elements are replaced - with replacement), while random.sample()
will not (once elements are picked, they are removed from the population to sample, so, once drawn the elements are not replaced - without replacement).
Note that here replaced (replacement) should be understood as placed back (placement back) and not as a synonym of substituted (and substitution).
To better understand it, let's consider the following example:
import random
random.seed(0)
ll = list(range(10))
print(random.sample(ll, 10))
# [6, 9, 0, 2, 4, 3, 5, 1, 8, 7]
print(random.choices(ll, k=10))
# [5, 9, 5, 2, 7, 6, 2, 9, 9, 8]
As you can see, random.sample()
does not produce repeating elements, while random.choices()
does.
In your example, both methods have results with repeating values because you have repeating values in the original sequence, but, in the case of random.sample()
those repeating values must come from different positions of the original input.
Eventually, you cannot sample()
more than the size of the input sequence, while this is not an issue with choices()
:
# print(random.sample(ll, 20))
# ValueError: Sample larger than population or is negative
print(random.choices(ll, k=20))
# [9, 3, 7, 8, 6, 4, 1, 4, 6, 9, 9, 4, 8, 2, 8, 5, 0, 7, 3, 8]
A more generic and theoretical discussion of the sampling process can be found on Wikipedia.