### Random number based off a percentage

Hey guys so Im building a text-based game that has enemies you create through OOP. The enemies attacks are in an array of 4 attacks and I create them like such
Enemy e;
e.setLevel(1); // sets enemy level 1 that sets its health, dmg, ect..

anyways so the enemy abilities has a variety of attacks like, dodge and slash.
Of course the dodge attacks output 0 damage, but I want this to be % based so when player.attacks(); I want the % that the enemy will choose their dodge attack much higher than their attack ability. But I don't want them to dodge every attack the player throws at them. Remember all the attacks are in an array. Thanks gys.
 ``1234567891011121314`` ``````double p = /*...*/; //p is the probability that the defender will dodge assert(p >= 0 && p <= 1); bool dodge_successful; if (p == 0) dodge_successful = false; else if (p == 1) dodge_successful = true; else{ double roll = prng(); assert(roll >= 0 && roll <= 1); //dodge_successful = roll > p; //EDIT: This is incorrect given the above statements. dodge_successful = roll < p; }``````
Last edited on
awesome thanks, gonna implement it
Oops. My bad. On line 12 the expression should be
`dodge_successful = roll < p;`
Topic archived. No new replies allowed.