Thursday, June 4, 2015

How do I create a weighted collection and then pick a random element from it?


I have a loot box that I want to fill with a random item. But I want each item to have a different chance of being picked. For example:



  • 5% chance of 10 gold

  • 20% chance of sword

  • 45% chance of shield

  • 20% chance of armor

  • 10% chance of potion



How can I make it so that I select exactly one of the items above, where those percentages are the respective chances of getting the loot?




No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...