Chance of attack/even happening

Started by
9 comments, last by Fingers_ 18 years, 11 months ago
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 :)
Advertisement
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
?

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.

Everything is better with Metal.

Quote:Original post by rpg_code_master
Well 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.

Quote:Original post by oliii

or better

int val = rand() % 100;

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

Thats what I did...

tiny error...
if ( rand( ) % 100 <= nPercentToAttack )

should be...
if ( rand( ) % 100 < nPercentToAttack )
This space for rent.
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?
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.
Quote:Original post by Kylotan
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.


ok, thanks
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.

This topic is closed to new replies.

Advertisement