Currently i'm working on a project where I need to code something in Python but stumbled upon some obstacles. I have an excel sheet with 692 items (stock keeping units) with their corresponding sales numbers. The items are in sequence from most sold till least sold. I calculated both the probability and cumulative probability for each item as can be seen in images 1 and 2 below:
Based on the cumulative probability and following formula: =INDEX(A$2:A$721;COUNTIF(H$2:H$721;"<="&RAND())+1)
randomly 20 items are selected from the 692 items to generate an order picking list, which can be seen in the following image:
Basically i want to do the same in Python. Thus, select a number of items from a large list based on their probability.
I found the following code, but not sure how to use it:
import random
def random_pick(some_list, probabilities):
x = random.uniform(0, 1)
cumulative_probability = 0.0
for item, item_probability in zip(some_list, probabilities):
cumulative_probability += item_probability
if x < cumulative_probability: break
return item
It would be great if someone could give me some directions in how to achieve this. Thanks in advance!