1. ## help with vectors please.

i'm trying to build a class that represents a shuffled deck of cards.
i start by creating and ordered deck but when i try to randomize it things start to go wrong and i cant figure why.

Code:
```#include <stdlib.h>
#include <iostream>
using namespace std;
#include "deck.h"

//deck::deck();
//{

//}

void deck::shuffle()
{
//gernerate an ordered deck of cards
for(int i=0; i<4; i++)
{
for(int j=1; j<14; j++)
{
m_cards.push_back(i);
m_suits.push_back(i);
m_cards[i] = j;
m_suits[i] = int(i + 3);
//	cout<<m_cards[i]<<m_suits[i]<<endl;

}
}
//randomize the deck
int randNum;
srand(5);
for(i = 52; i>0; i--)
{
randNum = rand() % i;
m_deck[i] = m_cards[randNum];
m_suitsArray[i] = m_suits[randNum];
m_cards.erase(m_cards.begin() + randNum);//erase the elements that resided at
m_suits.erase(m_suits.begin() + randNum);//loc randNumber so that no two cards
//can be put in the deck twice
cout<<m_deck[i]<<m_suitsArray[i]<<endl;
}

}```

2. You could also consider an STL solution - random_shuffle......looks like that's just what you are looking for!

Code:
```#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>
#include <algorithm>

void Print(const std::vector<int>& v)
{
for(std::vector<int>::const_iterator i = v.begin();
i < v.end();++i)
std::cout << *i << " ";

std::cout << std::endl;
}

int main(void)
{
const int MaxNumbers = 10;
std::vector<int> v;
std::srand(std::time(0));
for(int i = 0;i < MaxNumbers;++i)
v.push_back(i + 1);

Print(v);

std::random_shuffle(v.begin(),v.end());

Print(v);

std::random_shuffle(v.begin(),v.end());

Print(v);
}```

3. If so, move the srand(5) call to the start of main(), and ensure that it is only called ONCE
No, i just put in srand(5) as a quick test. The problem is that duplicate cards end up in the deck.

You could also consider an STL solution - random_shuffle......looks like that's just what you are looking for!
thanks i'll try it out. btw, wich header contains the random suffle method?

but i'd still like to know why my version dosn't work if any one cares to take a guess.

4. Originally posted by axlton
btw, wich header contains the random suffle method?

#include <algorithm> - as in my example

5. It looks like the problem in your code is that you aren't understanding what push_back does. It doesn't create a spot at the index of the int you pass in. It just creates a new entry in the vector at the end and initializes the value at that entry to the number you pass in. Because of this, all you need to do to initialize your ordered deck of cards is to push_back each number like below:
Code:
```//generate an ordered deck of cards
for(int i=0; i<4; i++)
{
for(int j=1; j<14; j++)
{
m_cards.push_back(j);	// switched to j
m_suits.push_back(i);
//			m_cards[i] = j;
//			m_suits[i] = int(i + 3);
}
}```

6. Later, you attempt to add these things to the "shuffled deck" m_deck. I don't know if you are initializing the size of m_deck and m_suitsArray elsewhere in your code, but if you're not, then your shuffle code won't work. You need to either push_back the randomly picked cards, or size/resize those two vectors to 52 before setting the entries in the shuffle loop.

7. thanks, although i've acutally found a much easier solution using an array of structs rather than vectors i plan to play around with this some more just so i can understand vectors better.