hi in my console rpg im makinh i want to have a critical strike chance. i made a stat called agility. every point in agility is .5% chance to critical strike. how would i make the computer do the math using some sort of protablity.
random ?
to expand, add up the agility points * .5 or divide by 2, whatever you fancy, and then get a random number out of 100, if it is the same number as the crit chance or less then the strike crits.
well the way I see it if you have 25% critical strike chance, then 1 out of every 4 hits will crit, however this should still be random and not: hit hit hit crit hit hit hit crit hit hit hit crit. Unless you want it to work in that fashion.
#include <ctime> // time()
#include <cstdlib> // srand() and rand()
bool critical_strike(int agility) {
srand ( time(NULL) ); // seeds rand() with a number based on the current system time.
if ( agility > rand() % 200 )
return 1;
elsereturn 0;
}
Here is one possible way of doing it if you're not used to using srand() and rand(). It is a function that takes agility as an argument, randomly determines whether a critical strike is made and then returns either 1 (hit) or 0 (miss).
I got to know, doesnt calling it once in main generate one seed for all references to it. Or is it like you say, you just need one seed and it generates random numbers off that.
Guys I know your 'helping'. But you have to be careful of the help you are giving, it has to be precise as possible or at least let the OP know your not 100% sure, or have any clue....
There are much better random generators out on the net for this type of thing. However if your going to use rand() more than once, then you should use srand() at the start of main, this may seem odd but it works.
edit: also in the above function that would still account for 0% crit, in which case I don't think there should be a chance... +1 would be better: