1import random
2
3# with replacement = same item CAN be chosen more than once.
4# without replacement = same item CANNOT be chosen more then once.
5
6# Randomly select 2 elements from set without replacement and return a list
7random.sample(set_name, 2)
1import random
2cakes = ['lemon', 'strawberry', 'chocolate']
3random.choice(cakes)
4# prints 1 randomly selected item from the collection of n items with
5# the probability of selection as 1/n
1# Basic syntax:
2import random
3random.sample(your_set, number_of_elements)
4# Where:
5# - number_of_elements is the number of elements you want to sample
6# without replacement from your_set
7# Note, the samples elements are returned as a list. Use set(the_list)
8# to convert back to a set
9
10# Example usage:
11your_set = {'a', 'bunch', 'of', 'unique', 'words'}
12random.sample(your_set, 1)
13--> ['words']
14
15random.sample(your_set, 3)
16--> ['words', 'of', 'a']