Need an array to be stored as a deck of cards, but random numbers instead.

I get this output:

1
1
1
1
5
2
7
2
3
5
11
3
13
7
5
4
0
6
0
5
7
11
0
6
0
13
9
7
0
10
0
8
11
0
0
9
0
0
13
10
0
0
0
11
0
0
0
12
0
0
0
13

When I run this function and cout cards:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void ShuffleTheDeck(vector<int> &cards, int decks)
{
	int count, cardcount, counttwo;
	cards.resize(decks*52);
	for(count=1;count<=decks;count++)
	{
		for(counttwo=1;counttwo<=4;counttwo++)
		{
			for(cardcount=1;cardcount<=13;cardcount++)
				cards[((count * counttwo) * cardcount) - 1] = cardcount;
		}
	}
	//random_shuffle(cards.begin(), cards.end());
}

While decks=1;
It is supposed to contain 4 of each card(1-13);

PLEASE TRY TO MODIFY THE ABOVE CODE BEFORE POSTING A COMPLETELY DIFFERENT ONE!
Last edited on
I get the same output when I declare a variable to go in: cards[_____]
Plant the seed

1
2
3
4
srand ( time(NULL) );

random_shuffle(cards.begin(), cards.end());


Last edited on
In line 10, count*counttwo*cardcount-1 is:

0 1 2 3 4 5 6 7 8 9 10 11 12
when counttwo is 1.

It is:
1 3 5 7 9 11 13 15 17 19 21 23
when counttwo is 2.

It is:
2 5 8...
when counttwo is 3.

I think you get the point.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void ShuffleTheDeck(vector<int> &cards, int decks)
{
	cards.resize(decks*52);
	
	for ( unsigned deckNo = 0; deckNo < decks; ++deckNo )
		for ( unsigned suitNo=0; suitNo < 4; ++suitNo ) 
			for ( unsigned faceNo=0; faceNo < 13; ++faceNo )
			{
				const unsigned cardIdx = deckNo*52 + suitNo*13 + faceNo ;
				cards[cardIdx] = faceNo+1 ;
			}

	//random_shuffle(cards.begin(), cards.end());
}


or:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void ShuffleTheDeck(vector<int> &cards, int decks)
{
	cards.resize(decks*52);
	struct card_generator
	{
		unsigned n ;

		card_generator() : n(0) {}

		int operator()() { return (n++ % 13) + 1 ; }
	};

	std::generate(cards.begin(), cards.end(), card_generator()) ;

	//random_shuffle(cards.begin(), cards.end());
}
Last edited on
Thank you cire. I completely overlooked that.
Topic archived. No new replies allowed.