C++ Random number generator, isnt random but always returns the same number -


hi im looking randnum generate number between 2-10, , number being taken away 15.

right now, every time program executed, number taken away 15, (perseushealth) 7. how fix this, , make random?

void desertpath() { int scorpianchoice; //the option player chooses. int maxhit = 10; //max hit scorpian can int minhit = 2; //min hit scorpian can  int randnum = rand()%(maxhit + minhit) + minhit;  int healthremaining = perseushealth - randnum; //health left in option1.   if(scorpianchoice == 1) {     cout << "you run under scorpians legs in hopes escape " << endl;     cout << "you hit scorpians sting " << randnum << " hp!";     cout << "you have " << healthremaining << "/15 hp left!" << endl;     cout << "you escape scorpian not without taking damage" << endl; } 

use srand initialize.

srand((unsigned)time(0)); 

also, think have not put brackets correctly:

int randnum = (rand()%(maxhit - minhit)) + minhit;  

Popular posts from this blog