Skip to content

Instantly share code, notes, and snippets.

@nguyyentantai
Created August 3, 2020 19:05
Show Gist options
  • Save nguyyentantai/751d29ea8c81d715826d0524c0a3577a to your computer and use it in GitHub Desktop.
Save nguyyentantai/751d29ea8c81d715826d0524c0a3577a to your computer and use it in GitHub Desktop.
random shuffle 52 cards for 4 player c++
// Example program
#include <iostream>
#include <vector>
#include <string>
#include <ctime>
using namespace std;
void printElements(vector<int> v){
for(auto it = v.begin(); it != v.end(); it++){
cout << *it << "\t";
}
}
void shuffle(vector<int> player[]){
int cards[52];
for(int i = 0; i < 52; i++){
cards[i] = i+1;
}
for (int i = 52; i >= 1;){
srand(time(NULL));
int j = rand()%i;
i--;
int temp = cards[i];
cards[i] = cards[j];
cards[j] = temp;
}
for(int i = 0; i < 52; i++){
cout << cards[i] << " ";
}
cout << endl;
for(int i = 0; i < 4; i++){
for(int j = 0; j < 13; j++){
player[i].push_back(cards[j+i*13]);
}
}
}
int main()
{
vector<int> player[4];
shuffle(player);
for(int i = 0; i < 4; i++){
printElements(player[i]);
cout << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment