#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
// Returns true if the item is in the list, otherwise false
bool checkInList(std::vector<int> &list, int value)
{
for (int i = 0; i < list.size(); ++i)
{
if (list.at(i) == value)
{
return true;
}
}
return false;
}
// min inclusive, max exclusive
// make sure to set a new seed before using to help make results more random. (std::srand)
int randomInt(int min, int max)
{
return rand() % (max - min) + min;
}
int main()
{
std::vector<int> list;
std::srand(std::time(0));
while (list.size() < 9)
{
int num = randomInt(1, 10);
if (!checkInList(list, num))
{
list.push_back(num);
}
}
for (int i = 0; i < list.size(); ++i)
{
std::cout << list.at(i) << std::endl;
}
return 0;
}
I liked sabreitweiser's answer for the simplicity, but it doesn't do exactly what you asked for.
The code I posted should be along the lines of what you're looking for.