### Rng help.

Hello, I am making a simple text based game, but I am getting stuck on some RNG things. I appreciate any help, I searched the forums, however i cannot find out what I am doing wrong... My code:

 ``1234567891011121314151617181920212223242526272829303132333435363738`` `````` while(Rat.Hp >= 1) { cout << "Attack (1): Attempt to grab its tail and slam the rat against the floor\n" << "Attack (2): Use your letter opener to slice the rat.\n\n"; cin >> P.Choice; //choice of attack and hit chance/dmg done if(P.Choice == 1) { chance = rand() % 1; //chance to hit is 50% if(chance == 1) P.A1 = 0; //Miss else if(chance == 0) P.A1 = 4; } else if(P.Choice == 2) { chance = rand() % 9; //chance to hit is 90% if(chance != 9) P.A1 = 1; else if(chance = 9) P.A1 = 0; //Miss } Rat.Hp - P.A1; cout << "You hit the rat for " << P.A1 << " damage!\n\n"; } ``````

I also included srand(time(0)); and other declerations needed.

the rand function doesnt seem to be working, everytime i use it, P.A1 = 1 or P.A1 = 4 based on the users choice.
P.A1 never equals 0.
Last edited on
If user choice is equal to 1, than your chance is always 0 (x % 1 is always 0), so P.A1 = 4.
If user choice equals 2, than chance is number in interval from [0..8], always different from 9 -> P.A1 = 1.

Your code should be something like it:
 ``1234567891011121314151617181920212223242526272829303132333435363738`` `````` while(Rat.Hp >= 1) { cout << "Attack (1): Attempt to grab its tail and slam the rat against the floor\n" << "Attack (2): Use your letter opener to slice the rat.\n\n"; cin >> P.Choice; //choice of attack and hit chance/dmg done if(P.Choice == 1) { chance = rand() % 2; //chance to hit is 50% if(chance == 1) P.A1 = 0; //Miss else P.A1 = 4; } else if(P.Choice == 2) { chance = rand() % 10; //chance to hit is 90% if(chance != 9) P.A1 = 1; else P.A1 = 0; //Miss } Rat.Hp - P.A1; cout << "You hit the rat for " << P.A1 << " damage!\n\n"; } ``````

Oh yea wow, thanks so much!
Topic archived. No new replies allowed.