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.
Asked
Active
Viewed 374 times
2 Answers
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
-
1I fixed my math. You just plug every number in to the formula 0.01 through 1.00 – Scott Chamberlain Jun 02 '18 at 16:39
-
The formula is `percent hit chance <= the value you want to test normalized to the 0 to 1 range.` – Scott Chamberlain Jun 02 '18 at 16:41