-5

I have a rocket wich have 49% hit ratio. What is the formula to calculate hit (true/false) for that rocket? I have many objects with different probabilities.

Sergey Efimov
  • 473
  • 1
  • 3
  • 16

2 Answers2

0
Random rand = new Random(); 

if(rand.Next(1, 101) <= rocketHitRatio)
//hit
else
//no hit
Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
0

Random.NextDouble() returns a number between 0.0 or greater and less than 1.0 . You just compare that result to your percent chance expressed as a double, if it is below or equal to the value of the hit chance you have a hit.

bool IsHit(Random rnd, double hitChance)
{
    return rnd.NextDouble() <= hitChance;
}

double rocketChance = 0.49;
bool hit = IsHit(_random, rocketChance);
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431