In C ++, there are different random number generators with different distributions. But in this case, it does not matter, because for such a simple goal rand() is quite adequate, although, of course, its range is not so great. And yet - srand(time(0)); - it's just adding randomness, nothing more, but not affecting the properties of the generator.
Simplest test
int chances(int value) { int count = 0; for(int i = 0; i < 1000; ++i) { if (rand()%100 < value) ++count; } return count; } int main(int argc, const char * argv[]) { srand(time(0)); for(int value = 0; value < 100; value += 10) { int count = chances(value); printf("%3d : %3d.%01d\n",value, count/10,count%10); } }
Gives a result on Visual C ++ 2015 (it is clear that with fluctuations from launch to launch)
0 : 0.0 10 : 11.3 20 : 22.1 30 : 29.7 40 : 40.7 50 : 51.5 60 : 59.2 70 : 70.6 80 : 79.0 90 : 89.9
Here you can see the result for GCC.
You can - if you want - use the option
std::default_random_engine u{}; std::uniform_int_distribution<> d{}; u.seed(std::random_device()()); // Аналог srand d(u,uniform_int_distribution<>::param_type{0,100}); // - аналог rand()%100
It is even more correct to use in this case
bernoulli_distribution b; // Замена вашего if (rand()%100 < critical_chance) if (b(u,std::bernoulli_distribution::param_type{critical_chance/100.0})) { ... };
if (rand()%100 > critical_chance) {...}written, then the condition will be satisfied in 73/100. Change the> sign to <=. - h86C1p