Skip to content

Commit e72e563

Browse files
authored
Create Shuffle a deck of cards
1 parent 9f9243b commit e72e563

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

Shuffle a deck of cards

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// C++ program for shuffling desk of cards.
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
// Function which shuffle and print the array
6+
void shuffle(int card[], int n)
7+
{
8+
// Initialize seed randomly
9+
srand(time(0));
10+
11+
for (int i=0; i<n ;i++)
12+
{
13+
// Random for remaining positions.
14+
int r = i + (rand() % (52 -i));
15+
16+
swap(card[i], card[r]);
17+
}
18+
}
19+
20+
// Driver code
21+
int main()
22+
{
23+
// Array from 0 to 51
24+
int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8,
25+
9, 10, 11, 12, 13, 14, 15,
26+
16, 17, 18, 19, 20, 21, 22,
27+
23, 24, 25, 26, 27, 28, 29,
28+
30, 31, 32, 33, 34, 35, 36,
29+
37, 38, 39, 40, 41, 42, 43,
30+
44, 45, 46, 47, 48, 49, 50,
31+
51};
32+
33+
shuffle(a, 52);
34+
35+
// Printing all shuffled elements of cards
36+
for (int i=0; i<52; i++)
37+
cout << a[i] << " ";
38+
cout << endl;
39+
40+
return 0;
41+
}

0 commit comments

Comments
 (0)