Get a random sample with replacement
In Python 3.6, the new random.choices() function will address the problem directly:
>>> from random import choices>>> colors = ["R", "G", "B", "Y"]>>> choices(colors, k=4)['G', 'R', 'G', 'Y']
With random.choice
:
print([random.choice(colors) for _ in colors])
If the number of values you need does not correspond to the number of values in the list, then use range
:
print([random.choice(colors) for _ in range(7)])
From Python 3.6 onwards you can also use random.choices
(plural) and specify the number of values you need as the k argument.
Try numpy.random.choice
(documentation numpy-v1.13):
import numpy as npn = 10 #size of the sample you wantprint(np.random.choice(colors,n))