# Chance of attack/even happening

This topic is 4995 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I wasn't really sure where to place this, I decided on in here because I is directly related to game programming. My question stems from the game Final Fantasy Tactics for Playstation. Anyone who has played the game will know what I mean. During a battle, when the player chooses to attack an enemy in some way, some information appears at the bottom of the screen, the expected damage to be dealt and a percent chance that the attack will be successful. What I can't figure out how to do, is the percent successful. How can I get it so that a percent chance is calculated and the success of the attack/event is dependent on it; the higher the percent, the more likely it is to succeed. I'm sure it is or can be quite simple, but I just can't quite seem to think of it. Any insight would be appreciated, thanks :)

##### Share on other sites
Well the actual percentage would depend on the attacking characters speed and accuracy and the monsters speed and evasion.

To incorperate this percentage, use this code:-

if ( rand( ) % 100 <= nPercentToAttack )    Attack( ); // the attack is successfull

##### Share on other sites
?

rand().

rand() gives a pseudo-random value in the range 0-RAND_MAX

so, to get the value in the range 0-100 (for a percent),

int val = (rand() * 100) / RAND_MAX;

or better

int val = rand() % 100;

that takes the modulo (the reminder of the division rand() / 100).

// 75 percent chance of success
if (val < 75)
{
//*wallop!*
//enemy gets hit in the face
}

there are lots of random and probabilistic functions you can engineer. rand() is just a standard C/C++ seeded ramdon number generator, so not totally random, nothing is (controversy!), but close enough.

##### Share on other sites
Quote:
 Original post by rpg_code_masterWell the actual percentage would depend on the attacking characters speed and accuracy and the monsters speed and evasion.

This,
[source lang = "cpp"]if ( rand( ) % 100 <= nPercentToAttack )    Attack( ); // the attack is successfull

is exactly what I had in mind, I just couldn't quite put my thumb on how to implement it. I was right tho, it was simple lol; thanks guys.

##### Share on other sites
Quote:
 Original post by oliiior better int val = rand() % 100; that takes the modulo (the reminder of the division rand() / 100).

Thats what I did...

##### Share on other sites
tiny error...
if ( rand( ) % 100 <= nPercentToAttack )

should be...
if ( rand( ) % 100 < nPercentToAttack )

##### Share on other sites
I was thinking about this for a little while and was wondering if the nPercentToAttack could be defined a little more. Would this be the value of 'percent failure', since we are testing to see if the random value is less than nPercentToAttack?

##### Share on other sites
No, it's the percentage of success. The smaller the percentage gets, the smaller the chance of you being able to pick a random number that is less than it.

##### Share on other sites
Quote:
 Original post by KylotanNo, it's the percentage of success. The smaller the percentage gets, the smaller the chance of you being able to pick a random number that is less than it.

ok, thanks

##### Share on other sites
Quote:
 rand( ) % 100

Is actually quite bad, besides using rand() as a PRNG.
Say that the rand function actually generates 15 random bits of data, using the % operator give the numbers [0, 67] a slight favour (i.e more likely to appear, around 0.3% more often).

Like someone suggested, use something like this instead:

bool doAttack(int percentage){   return ((rand()* 100) / RAND_MAX) < percentage;}

It's still not very good since there are numbers that is more likely to appear.
Do consider to use a better PRNG, rand gives very poor results and I don't expect you to call the PRNG more than a few 100 times each frame?

I'm using this PRNG, which is way better!
My implementation is about 6 times slower than the normal rand which is quite acceptable given the quality.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 14
• 14
• 10
• 9
• 11
• ### Forum Statistics

• Total Topics
634096
• Total Posts
3015492
×