If you are using C++, I can help you:
First, you will have to include the header file cstdlib in the beginning of your code.
Next, you must 'seed' the random number generator. This means you will be telling the generator where to start generating 'random' numbers. If you do this by giving the generator the same value every time, however, you will get the same 'random' numbers. So we seed the generator according to your computer's system time.
To seed the generator properly, use the following line of code:
- srand( (unsigned) time( 0 ) );
Now, we are ready to use the rand() function. rand() returns an integer between 0 and INT_MAX (a value somewhere around 2,147,483,647). Obviously, you want to limit these values to a certain range. In order to do this, use the modulus division operator - '%'. You can declare a variable - such as gold or steps in your example - and initialize it to rand() % RANGE + MIN_VALUE, where RANGE is the highest number of gold/steps you want to have minus the lowest value, and MIN_VALUE is the lowest value you wish to have. Why do we have to do this?
Suppose I want to generate a number between 1 and 100. I could use
But this will give me numbers between 0 and 99 (because 100 % 100 = 0, 200% 100 = 0, etc). In order to get the value between 1 and 100, I can add one to the overall value:
- int num = rand() % 100 + 1;
If I wanted to generate a number between 50 and 75, I would use the following statement:
- int num = rand() % (75 - 50) + 50;
Hope this helps!