Random Num Generator

Hello,
I am trying to make a random number generator to simulate a critical hit if the number generated is from 0-critical amount. However, every time i run the code, despite being within the parameters set by the if statements, it always runs through as a critical hit and never a regular hit.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <iomanip>
#include <ctime> 
#include <cstdlib> 
using namespace std;

int main(){

	srand((unsigned)time(0));
	int R;
	double critical, damage, hit;
	critical = 30.00;
	damage = 10.00;

// Attempt 1
R = (rand()%100);
cout << R << endl;

if (R>=0||R<=critical){
	cout << "Critical hit! 2x damage" << endl;
	hit = damage*2;
	cout << hit << endl;
}

else if (R>critical){
	cout << "Regular hit. 1x damage\n" << endl;
	hit = damage;
	cout << hit << endl;
}

// Attempt 2
cout << endl;
R = (rand()%100);
cout << R << endl;

if (R>=0||R<=critical){
	cout << "Critical hit! 2x damage" << endl;
	hit = damage*2;
	cout << hit << endl;
}

else if (R>critical){
	cout << "Regular hit. 1x damage\n" << endl;
	hit = damage;
	cout << hit << endl;
}

// Attempt 3
cout << endl;
R = (rand()%100);
cout << R << endl;

if (R>=0||R<=critical){
	cout << "Critical hit! 2x damage" << endl;
	hit = damage*2;
	cout << hit << endl;
}

else if (R>critical){
	cout << "Regular hit. 1x damage\n" << endl;
	hit = damage;
	cout << hit << endl;
}

// Attempt 4
cout << endl;
R = (rand()%100);
cout << R << endl;

if (R>=0||R<=critical){
	cout << "Critical hit! 2x damage" << endl;
	hit = damage*2;
	cout << hit << endl;
}

else if (R>critical){
	cout << "Regular hit. 1x damage\n" << endl;
	hit = damage;
	cout << hit << endl;
}
}


There are four attempts to save time in getting a good result. Please help.

Thanks,
CloudIsland
What's for condition R>=0 in each if (R>=0||R<=critical) ? As you defined R it will result a number between 0 and 99 so this condition is superfluous. Delete it please.
Last edited on
if (R>=0||R<=critical)

Look it this condition. Can you think of any values of R that would cause it to ever be false?
Last edited on
@MikeyBoy

if you want to keep the condition R >= 0 then if (R>=0||R<=critical) must be modified:

if (R>=0 && R<=critical) and also rests superfluous but works!...Please try both cases and see.
I think you want to direct that to the OP, not me :)
Obviously, if I'm wrong is my fault, of course!
@condor
@MikeyBoy

Thanks mates, I guess thats why you don't program at 2am aha. Took out the R>=0 and everything works perfectly!
Topic archived. No new replies allowed.