Re-initialising srand without exiting

Currently creating a basic RPG as a side hobby.
I have two classes which will battle and have decided to give them integer values for damage and apply a modifier using srand(time(0)).
When running the code, srand produces 1 number as a modifier and maintains it, is there a way to have srand generate a new sequence to give a different attack modifier between 0.5x and 2x or are there alternatives?
Thanks in advance!!
PS, this itsn't all of the code, just an fyi.
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
class Warrior:public Player{
	public:
	double modif = ((1+rand()%4)/2);
	Warrior(){ AttackPower = 30; health = 120; }
	Warrior(int AttackPower, int health){}   
	void Attack(Enemy &enemy){ 
		cout << "Warrior attacks to deal " << AttackPower*modif << " damage" << endl;
		
		enemy.health -= AttackPower;
		if(enemy.health <= 0){
		cout << "Enemy is dead" << endl;
		}
	}
};

int main(){
	srand(time(0));
	Warrior warrior1;
//	Warrior warrior2(50, 150);
	Ninja ninja1;
//	Ninja ninja2(35, 125);
	cout << "Warrior Health - \t" << warrior1.health << "\t\tNinja Health - \t" << ninja1.health << endl;
	
	ninja1.Attack(warrior1);
	cout << "Warrior now has " << warrior1.health << " health" << endl;
	cout << endl;
	cout << "Warrior Health - \t" << warrior1.health << "\t\tNinja Health - \t" << ninja1.health << endl;

	warrior1.Attack(ninja1);	
	cout << "The ninja now has health: " << ninja1.health << endl;
	cout << endl;
	system("pause");
}
Last edited on
srand(...) does not 'produce' a number, it determines the start condition for calculation the pseudo random numbers.

rand() is suppose to provide different number as much as the algorithm allows it.

When you provide the same number to srand(...) the follow up rand() numbers are also the same.
TL;DR: You're wasting time. Just call rand() to get a random number. What you do to transform that number into your desired range comes after the obtaining.

Hope this helps.
Topic archived. No new replies allowed.