I am working on a schedule generator, and I need to randomly put two days off into it. I came up with a solution, however I noticed that randinit method can put two days off on one day, so this option doesn't work for me.
import random
schedule = {1: "9:00 - 17:00", 2: "9:00 - 17:00", 3: "16:00 - 00:00", 4: "16:00 - 00:00", 5: "16:00 - 00:00", 6: "16:00 - 00:00", 7: "16:00 - 00:00"}
days_off = {random.randint(1, 7): "X", random.randint(1, 7): "X"}
schedule.update(days_off)
So I am thinking that there should be an easier way to make it work without hardcoding, but I cannot really find it.
Will appreciate any help!
CodePudding user response:
What you are looking for is random.sample()
, which allows you to randomly select multiple different values from a given population:
for d in random.sample(range(1, 7 1), k=2):
schedule[d] = "X"
CodePudding user response:
Another answer with numpy:
import numpy as np
for day_off in np.random.choice(np.arange(7), size=2, replace=False):
shedule[day_off 1] = "X"