mardi 24 janvier 2017

Unique Random Number between 0 and 9

I am trying to generate a unique random number between 0 and 9. The same number cannot be generated twice and the function will be ran 9 time (until all the 9 numbers are used.) Here is the latest way I have been trying to do this:

int uniqueRandomInt(int x) {

    std::vector<int> usedRandoms;
    int random = x;

    //Iterate vector
    for (unsigned int i = 0; i < usedRandoms.size(); i++) {
        //if passed value is in vector
        if (random = usedRandoms[i]) {
            uniqueRandomInt(random);
        }
        else {
            //If unique rand found put into vector
            usedRandoms.push_back(random);
            return random;
        }
    }
}

Calling it in another function using:

cout << uniqueRandomInt(-1) << endl;

Result I am getting is:

17801152 (Changes every time the function is called)

Am I going about this totally wrong? I did try other ways but with no luck and this is where I'm currently at. Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire