Calculate safe amount of items for weighted random picks

26 Views Asked by At

Say I want to create lootboxes of 5 random items, weighted by rarity: common, rare, epic.

Given I've got 10 unique common items, 6 rare items and 3 epic with weights: 0.7 (common), 0.2 (rare) and 0.1 (epic) and I want to create the items upfront to allow the creation of 1000 lootboxes. Also, I need to guarantee that any lootboxes will have at least 1 rare items.

What would be the formula to calculate how many items of each I need to create so I don't fall short of items when creating the lootboxes?

The "brute force" way would be to create at least the maximum amount of each item those lootboxes might have if the random process returned always the same item in the class. But is there a way to optimize it?