lundi 27 juillet 2015

Using rand() to generate numbers around a variable?(C++)

I am making a small text-based game in c++ called "House Evolution" for fun. The game consists of 'searching under the couch cushions' to gain credits. When you search, the game is supposed to generate a random number anywhere from creditRate-5 to creditRate+5. How would I go about doing this using the rand() function, no matter what number creditRate is? Here is example code:

#include <iostream>
#include <unistd.h>
#include <string>
#include <cstdlib>
#include <math.h>

int main()
{
    int creditRate = 30; // Just for example.
    int credits;
    int searching;

    while (1) {
        // Yes, I know, infinite loop...
        std::cout << "Credits: " << credits << std::endl;
        std::cout << "Type any key to search for credits: " << std::endl;
        std::cout << "Searching...\n";
        usleep(10000000); // Wait 10 seconds

        searching = rand(?????????); // Searching should be creditRate-5 to creditRate+5

        std::cout << "You found " << searching<< " credits\n";
        credits += searching;
    }
}




Aucun commentaire:

Enregistrer un commentaire