Removing Duplicates in random set of numbers

The purpose of this program is to generate 10 sets of 6 random numbers and remove any duplicates in each set. I'm not too familiar with arrays so I'm having trouble making the if statement :(


int main()
{
int i,j;
int removerepeat; int A[]; int 6;


for (i=0; i<10; i++)
{
for (j=0; j<6; j++)
{
cout<<" "<< rand()%74;
if (A[i]==A[j])
return 1;
}
cout<<endl<<endl;

}

cin.get();
return 0;
}
Hello,

This is in Qt/C++, but it will give you an idea of how to get rid of duplicates, don't put them in the set in the first place.

Regards
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    QList<uint> numList;
    uint num;
    bool duplicate;
 
    for (uint i = 0; i < qty; i++)
    {
        do {
            num = (rand() % (maxNum - minNum + 1) + minNum);
            if (numList.contains(num))
            {
                duplicate = true; // skip duplicates
            }
            else
            {
                numList.append(num);
                duplicate = false;
            }
        } while (duplicate); //true
    }
Last edited on
What do you mean by "no duplicates"?
A) Each set has 6 unique values
or
B) All 60 values are unique

Think about a deck of cards. Each card is unique. Shuffled deck has the cards in random order. When you take cards from the deck, you do get unique and random values.

Your "cards" seem to be integers from 0 to 73. You would need a "deck" of 74 integers.

Btw, int A[]; is a syntax error; how many elements does A have? It has to be a known constant value.

How to shuffle? See http://www.cplusplus.com/reference/algorithm/shuffle/
It has an example.
Topic archived. No new replies allowed.