Sign in to follow this  

Chance of attack/even happening

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
Share on other sites
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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
I doubt 0.3% is going to make the slightest bit of difference. You'd have to "roll the dice" thousands upon thousands of times before the statistical effect will be as significant as the natural random variance. Who's going to write down every die roll they make anyway (not that most games will even display it)?

In a game system roll that isn't directly seen or controlled by the player you can even use a repeating list of pseudo-random numbers and nobody will be any the wiser (e.g. take numbers 0-99 and shuffle them, gets you a perfect distribution too :) ).

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this