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


i have 5 classes (legendary egg, rare egg, common egg, dragon food, gold). items inherited item class.

supposedly, output user playing gacha machine can of items. want set chance percentage of getting specific items won't easy rarer items.

legendary egg = 1% rare egg = 10% common egg = 20% dragon food = 20% gold = 29% 

what efficient way this? put items in array @ first , used rand()% realized couldn't set chance of getting them. thought of using like

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

but felt bit inefficient because told avoid blocks of if else.

*the items in own (separate) class because have different abilities

the modern c++ way see

#include <iostream> #include <map> #include <random>  int main() {     std::random_device rd;     std::mt19937 gen(rd());     std::discrete_distribution<> d({1, 10, 20, 20, 29, 20});      std::map<int, int> m;     for(int n=0; n<10000; ++n) {         ++m[d(gen)];     }     for(auto p : m) {         std::cout << p.first << " generated " << p.second << " times\n";     } } 

simply call d(gen)to 1 item desired distribution ( 0 = 'legendary egg' , on).

note: values weights. talked percent. should add 100, added 20%.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

.htaccess - ERR_TOO_MANY_REDIRECTS htaccess -