I have the following list:
priority=['Emergency','High','Normal','Low']
I need to generate a list of 15000 values in the list above, with frequency of each of them being 10%,20%,30%,40%. Can somebody show me how to do it?
I have the following list:
priority=['Emergency','High','Normal','Low']
I need to generate a list of 15000 values in the list above, with frequency of each of them being 10%,20%,30%,40%. Can somebody show me how to do it?
You can use random.random(), and assign indexes based on the range of the random number.
In [21]: priority=['Emergency','High','Normal','Low']
In [22]: idx = lambda x: 0 if x < 0.1 else (1 if x < 0.3 else (2 if x < 0.6 else 3))
In [23]: import random
In [24]: [priority[idx(random.random())] for _ in range(15000)]
Out[24]:
['Normal',
'High',
'Emergency',
'Emergency',
'High',
.
.
In the above, I've relied on the order of the elements within your list to generate indexes, such that for 0 to 0.1 it is the first (10%), 0.1 to 0.3 its the second (0.3 - 0.1 will cover 20%), and so on.
Probably the easiest way is to use choice
function of numpy.random.
import numpy as np
priority=['Emergency','High','Normal','Low']
b = np.random.choice(priority, 150, replace=True, p=[0.1, 0.2, 0.3, 0.4])
print(b)
Built-in random.choices
does the same thing but it is not available <3.6.