I want to generate a list of random integers less than 10 witch are not the same ( different integers )for example ( 0,3,1,5,8). but the code that I have written has a problem and first two integers always are the same. that would be great if you code tell me the mistake of my code or offer me another way to do that.
vector<int> rand_list(10, 11); //there is ten integers of 11
for (int i = 0; i < 5; i++) // here we make 5 different integers
{
srand(time(NULL));
int r = rand() % 10;
int check = 0;
for (check; check <= i; check++)
{
if (r == rand_list[check])
{
srand(time(NULL));
r = rand() % 10;
check = 0;//I think this line don't force the second loop for to start again.
}
}
rand_list[i] = r;
}
here I expect rand_list with 5 different integers and the other items must be 11 but first two integers are always the same!
Aucun commentaire:
Enregistrer un commentaire