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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…