random selection python code example
Example 1: sample 1 item from array python
import random
cakes = ['lemon', 'strawberry', 'chocolate']
random.choice(cakes)
Example 2: python choose random sample from list
import random
sequence = [i for i in range(20)]
subset = random.sample(sequence, 5)
print(subset)
Example 3: choice random python
import random
list = [20, 30, 40, 50 ,60, 70, 80]
sampling = random.choices(list, k=4)
print("Randomly selected multiple choices using random.choices() ", sampling)
Example 4: random picker in python
import random
x_dict = {30:60, 20:40,10:20}
key = random.choice(list(x_dict))
print (key)
print (x_dict[key])
print (key,"-", x_dict[key])