### How do I create a variable that chooses a random number each time I use it.

Hello. Currently I am working on a text based rpg game in C++. During an encounter I want a monster to give damage in between 1 and 4. Then I want this number to be subtracted from the players total hp. The problem is I need this number to change every time it is used.

Example:

Monster attacks player inflicting (random number between 1 and 4)
Player's total hp is now (player's hp - random number chosen)

Monster attacks player again inflicting (new random number between 1 and 4)
Player's total hp is now (player's hp - new random number)

How would I go about doing this. Any help would be much appreciated.
You can use the expression `(rand() % 4) + 1` to get a random number from 1 to 4 inclusive. You will need to include the header cstdlib and seed the random number generator with srand() before using it.
Something like this...

 ``1234567891011121314151617181920212223242526272829`` ``````#include #include using namespace std; //Declare function int RandomDamage(int,int); int main() { //Pick a seed for random based off current //time so each run will produce different results srand(time(0)); int damage = RandomDamage(1,4); cout << "Damage dealt: " << damage << "\n"; system("pause"); return 0; } int RandomDamage(int minDamage, int maxDamage) { int r = rand(); int range = (maxDamage - minDamage) + 1; r = (r % range) + minDamage; return r; }``````

HellfireXP i tried your code and the problem is i need the random number to change after each use for instance the first time it is 4 and the second it is 1.
Last edited on
Do you understand how variables work? If you do then the problem with your code should be obvious.
Each time you call the function RandomDamage() it should return a different number. The example above only runs one time, but if you restart it a few times, you should see different results.

I use Visual C++ 2010 Express as my IDE and the above code works just fine for me.
Last edited on
Firedraco I have a basic knowledge of them but I am fairly new with C++ so I am sure there is stuff I do not know yet.
Small change give you 10 damages:
 ``1234567891011121314151617181920212223242526272829303132`` ``````#include #include #include using namespace std; //Declare function int RandomDamage(int,int); int main() { //Pick a seed for random based off current //time so each run will produce different results srand(time(0)); for (int i=0; i<10; i++) { int damage = RandomDamage(1,4); cout << "Damage dealt: " << damage << "\n"; } system("pause"); return 0; } int RandomDamage(int minDamage, int maxDamage) { int r = rand(); int range = (maxDamage - minDamage) + 1; r = (r % range) + minDamage; return r; }``````

example output:
 ```Damage dealt: 2 Damage dealt: 3 Damage dealt: 3 Damage dealt: 3 Damage dealt: 1 Damage dealt: 3 Damage dealt: 2 Damage dealt: 2 Damage dealt: 4 Damage dealt: 2 ```
Ok what if i want the damage to be dealt (between 1 and 4) then the player to attack and then a new damage dealt (between 1 and 4). Don't include the player attacking in the code. It is just used as an example to show that i need the attacks to be spaced out.
Last edited on
int RandomDamage() is a function. So it can be called anytime you need it in your code...

So you can call RandomDamage(1,4) in one location in your code, then call it again later... or let's say you encounter a stronger monster that does damage 5-8, then you'd simple call RandomDamage(5,8).

In other words, the RandomDamage() function is flexible for any damage amounts and can be called multiple times from anywhere in your code.

If this is still confusing, I'd recommend you check this out: http://www.cplusplus.com/doc/tutorial/functions/
Last edited on
I wouldn't recommend using the following, but it's more in line with what the OP was asking for in a literal fashion.

 ``1234567891011121314151617181920212223`` ``````#include #include class RandNum { public: RandNum(int min, int max) : _gen(std::random_device()()), _dist(min, max) {} operator int() { return _dist(_gen) ;} private: std::default_random_engine _gen ; std::uniform_int_distribution<> _dist ; }; RandNum randomDamage(1,4) ; int main() { for (unsigned i=0; i<10; ++i) std::cout << randomDamage << '\n' ; }``````
I'm sorry. I am not very good at functions.

What i want is it so whenever i call RandomDamage () it is a different number. However since I do not know how many attacks it will make during a fight i can not name a variable every time i want to use RandomDamage(). I need a variable that each time it is used it changes numbers between 1 and 4.

Example:

int RandomNumber = RandomDamage(1,4);
int hp = 20;

while( hp>0){
Cout << "Monster deals " << RandomNumber << "damage.";
cout << "Your hp is now " << hp-RandomNumber << ".";
//lets say RandomNumber was equal to 2
hp=hp-RandomNumber
}

Next the player will attack and it will restart. However when the loop restarts the integer RandomNumber needs to be a random number between 1 and 4, not 2 again.
Last edited on
Everytime you call RandomDamage() you will get a new number. In the code you posted, a minor tweak (move location of int RandomNumber = RandomDamage(1,4)) and you'll have the answer you're looking for...

 ``123456789`` ``````int hp = 20; while( hp>0){ int RandomNumber = RandomDamage(1,4); Cout << "Monster deals " << RandomNumber << "damage."; cout << "Your hp is now " << hp-RandomNumber << "."; //lets say RandomNumber was equal to 2 hp=hp-RandomNumber; }``````

So each time the while() loop wraps around, the RandomDamage() function will pass a NEW number between 1 and 4 to the RandomNumber integer variable.
Last edited on
Thank you so much. It works beautifully now.

Sorry if I was being dumb and did not understand what you were going at before.
On the forums is there something i click to show the problem has been solved or do i just leave it as is.

Thank you again.

Edit: Nevermind found the solved button
Last edited on
I believe on the very top of your post there is a button that says, "Solved" or something to that effect.
Topic archived. No new replies allowed.