+ 1

I'm having trouble generating a random number

I'm making a game in which the user is given a random number between 1 and 4, which depending on the number will roughly output the amount of damage you've done(eg. 1 would be small damage).this number would then be multiplied by 10 and subtracted from the enemy health.the problem is, when I run it, the enemy always takes 10 damage, meaning that i keep getting 1 each time. https://code.sololearn.com/crKaI3GC953C/?ref=app

26th Jul 2017, 8:21 PM
X-1
X-1 - avatar
2 Réponses
+ 8
https://code.sololearn.com/cDvxiAXwCT9A/?ref=app this is what they mean with rand() i put the globals in a blank namespace for you for now. its better than straight globals (which arent that bad when you are learning) but I will demonstrate putting all this into classes for you when i get home if you would like
26th Jul 2017, 9:51 PM
jay
jay - avatar
+ 1
Don't put srand() in global scope, put it in main instead, there is no need for it to be global and it is wrong anyway ( won't compile ). Don't put variables in global scope as well: int p_health = 100; int e_health = 100; int p_att = rand()%4; int e_att = rand()%4; rand() is called before srand() in main and you end up with the same number every time. Put the variables back in main like you had before. (after srand()) Use function parameters to pass them along instead. You say you want to generate a random number between 1 and 4. rand()%4 generates numbers between 0 and 3. You should use a class instead as well, it makes things much easier. Like variable accessability.
26th Jul 2017, 8:53 PM
Dennis
Dennis - avatar