making a critical strike chance

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.
Last edited on
closed account (j3bk4iN6)
maybe this will help
http://www.cplusplus.com/reference/clibrary/cstdlib/srand/
yes i suppose i could come up with something using random numbers.
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.
closed account (jLNv0pDG)
1
2
3
4
5
6
7
8
9
10
11
12
13
#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;
	else
		return 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).
Not a good idea to seed it every time you call the function. Just call it once at the start of main.
closed account (j3bk4iN6)
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.
closed account (jLNv0pDG)
srand ( time(NULL) );

Yes, that really should go once at the start of main. I was just trying to put everything into one function to demonstrate how it was used.
Last edited on
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <ctime>	// time()
#include <cstdlib>	// srand() and rand()
#include <iostream>
using namespace std;

bool critical_strike(int agility) 
{
   if ( agility >= (1+ rand() % 200 ))
      return 1;
   else
      return 0;
}

int main()
{
   srand ( time(NULL) );
   int crit=25;
   bool gotCrit;

   gotCrit = critical_strike(crit);
   cout << gotCrit << endl;

   return 0;
} 
Last edited on
closed account (j3bk4iN6)
so the s in srand is seed rand?
correct: http://www.cplusplus.com/reference/clibrary/cstdlib/srand/
clicking the link above will show uses that may be helpful.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* srand example */
#include <ctime>
#include <cstdlib>	
#include <iostream>

int main ()
{
  printf ("First number: %d\n", rand() % 100);
  srand ( time(NULL) );
  printf ("Random number: %d\n", rand() % 100);
  srand ( 1 );
  printf ("Again the first number: %d\n", rand() %100);

  return 0;
}


OUTPUT
1
2
3
4
5

First number: 41
Random number: 13
Again the first number: 41
Last edited on
Topic archived. No new replies allowed.