Populating a deck of cards not working

I am trying to randomly generate a deck of cards. The code I am using is almost correct, however it is duplicating one random card. In other words, I can generate 50 unique cards but I still have 2 that are the same and I can't figure out why just 2 cards are slipping through.

Here is the part of my code for generating the cards:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
int x = 1;
bool sameCard = false;

srand(time(0));

randomDeck[0] = rand() % 52 + 5;

while(x < 52)
{
	randomDeck[x] = rand() % 52 + 5;

	sameCard = false;

	for(int y = 0; y < x; ++y)
	{
		if(randomDeck[x] == randomDeck[y])
		{
			sameCard = true;
		}
	}

	if(sameCard == false)
	{
		++x;
	}
}


Any help is appreciated.
What makes you think you're generating a duplicate card? If I add enough code to get yours to compile and sort the resulting array to make duplicates obvious, I don't get any duplicates.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <random>
#include <algorithm>

void display( int* a, unsigned size, unsigned per_line)
{
    for ( unsigned i=0; i<size; ++i )
        std::cout << a[i] << ((i+1) % per_line ? '\t' : '\n') ;
}

int main()
{
    int randomDeck[52] ;

    int x = 1;
    bool sameCard = false;

    srand(time(0));

    randomDeck[0] = rand() % 52 + 5;

    while(x < 52)
    {
        randomDeck[x] = rand() % 52 + 5;

        sameCard = false;

        for(int y = 0; y < x; ++y)
        {
            if(randomDeck[x] == randomDeck[y])
            {
                sameCard = true;
            }
        }

        if(sameCard == false)
        {
            ++x;
        }
    }
    std::sort(randomDeck, randomDeck+52) ;
    display(randomDeck, sizeof(randomDeck)/sizeof(randomDeck[0]), 4) ;
}
5       6       7       8
9       10      11      12
13      14      15      16
17      18      19      20
21      22      23      24
25      26      27      28
29      30      31      32
33      34      35      36
37      38      39      40
41      42      43      44
45      46      47      48
49      50      51      52
53      54      55      56
Topic archived. No new replies allowed.