Answers for "python random.choices vs random.sample"

1

python random.choices vs random.sample

ll = list(range(10))

#random.sample only picks a number from the list ONCE
print(random.sample(ll, 10))
# [6, 9, 0, 2, 4, 3, 5, 1, 8, 7]

#random.choices can pick the same number twice or more
print(random.choices(ll, k=10))
# [5, 9, 5, 2, 7, 6, 2, 9, 9, 8]
#     ↑                 ↑  ↑
Posted by: Guest on January-31-2022

Python Answers by Framework

Browse Popular Code Answers by Language