from numpy import random
# weight random choices
# 10% chance of 2
# 30% chance of 4
# 60% chance of 6
# 0% chance of 8
# return array of 5 items of random choice from a list
x = random.choice([2, 4, 6, 8], p=[0.1, 0.3, 0.6, 0.0], size=(5))
print(x)
# output [4 6 4 6 4]
# 10% chance of 3
# 30% chance of 5
# 60% chance of 7
# return 2d array 3 * 5 of random chocies
x = random.choice([3, 5, 7, 9], p=[0.1, 0.3, 0.6, 0.0], size=(3, 5))
print(x)
# output
# [[7 7 7 7 7]
# [7 3 3 5 3]
# [7 7 7 7 7]]
aa_milne_arr = ['pooh', 'rabbit', 'piglet', 'Christopher']
>>> np.random.choice(aa_milne_arr, 5, p=[0.5, 0.1, 0.1, 0.3])
array(['pooh', 'pooh', 'pooh', 'Christopher', 'piglet'], # random
dtype='<U11')
np.random.choice(5, 3)
array([0, 3, 4]) # random
>>> #This is equivalent to np.random.randint(0,5,3)
numpy.random.Generator.choice
method
random.Generator.choice(a, size=None, replace=True, p=None, axis=0, shuffle=True)
Generates a random sample from a given array