I am trying to seed a vector with 52 values ranging from 1 to 52. I already have the code written to seed the vector but I cant figure out how to keep from repeating the same values from being seeded. This is for a five card stud game.
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<string>
#include<vector>
#include<ctime>
using namespace std;
int main()
{
int cardNum = time(0);
srand(cardNum);
vector<int> cardDeck(52);
for(int i = 0; i < 52; i++)
cardDeck[i] = rand() % 52 + 1;
for(int i = 0; i < 52; i++)
cout << cardDeck[i] << endl;
Hello, there are quite a few ways to accomplish this. Here is a 'modern' approach using standard constructs found in the <algorithm> header. Alternatively, you will have to explicitly check for duplicates each time you try to insert a new value (which might involve a nested for-loop).
Thank you Luke and Cubbi. I have only taken C++ 1 in school and am taking C++ 2 next semester. I am using my holiday break to further my understanding of C++ language by creating card games. Both of your algorithms are great pieces for me to explore .