vendredi 28 juillet 2017

Setting Chance of getting Items for Random (C++)?

I have 5 classes (Legendary Egg, Rare Egg, Common Egg, Dragon food, Gold). All items that are inherited from an Item class.

Supposedly, the output is that the user is playing a gacha machine where they can get any of those items. But I want to set the chance percentage of getting specific items so that it won't be so easy to get the rarer items.

Legendary Egg = 1%
Rare Egg = 10%
Common Egg = 20%
Dragon Food = 20%
Gold = 29%

What is an efficient way to do this? I put all the items in an array at first and used rand()% but I realized that I couldn't set the chance of getting them. I thought of using something like

if (value < 0.1){
std:: cout << "You got a legendary egg!";
}

but I felt that it would be a bit inefficient because I was told to avoid blocks of if else.

*The items are in their own (separate) class because they have different abilities




Aucun commentaire:

Enregistrer un commentaire