Random card from shuffle

How can I get a random card from a pack of cards. I have tried to write a shuffle using the srand but it gives me a random number followed by a letter, like 00712E.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
  #include "pch.h"
#include <iostream>
#include <cstdlib> // for seed
#include <ctime> // for time

using namespace std;

#define NUMBER_OF_CARDS 52

enum SUIT {HEARTS, DIAMONDS, CLUBS, SPADES};

struct Card {
	SUIT suit; // H, D, S, C
	int value; // A, 2, 3, 4, 5, 6, 7, 8, 9, 10
	int score; // A = 1 2 = 2, Q = 10

};

Card Pack[NUMBER_OF_CARDS];
void SetupPack();
void SetupHearts();
void SetupDiamonds();
void SetupClubs();
void SetupSpades();

void Play(); //play blackjack


void ShufflePack(); //shuffle deck of cards to make random



int main()
{
	
}



void Play()
{

	ShufflePack();

	
		


}


void SetupPack()
{
	SetupHearts();
	SetupDiamonds();
	SetupClubs();
	SetupSpades();
}

void SetupHearts()
{
	for (int loop = 0; loop <= 12; loop++)
	{
		Pack[loop].suit = HEARTS;
		Pack[loop].value = loop + 1;
		Pack[loop].score = loop + 1;

	}
	//score for Jack, Queen, King
	Pack[10].score = 10;
	Pack[11].score = 10;
	Pack[12].score = 10;
}

void SetupDiamonds()
{
	for (int loop = 13; loop <= 25; loop++)
	{
		Pack[loop].suit = DIAMONDS;
		Pack[loop].value = loop + 1;
		Pack[loop].score = loop + 1;

	}
	//score for Jack, Queen, King
	Pack[23].score = 10;
	Pack[24].score = 10;
	Pack[25].score = 10;
}


void SetupClubs()
{
	for (int loop = 26; loop <= 38; loop++)
	{
		Pack[loop].suit = CLUBS;
		Pack[loop].value = loop + 1;
		Pack[loop].score = loop + 1;

	}
	//score for Jack, Queen, King
	Pack[36].score = 10;
	Pack[37].score = 10;
	Pack[38].score = 10;
}

void SetupSpades()
{
	for (int loop = 39; loop <= 51; loop++)
	{
		Pack[loop].suit = SPADES;
		Pack[loop].value = loop + 1;
		Pack[loop].score = loop + 1;

	}
	//score for Jack, Queen, King
	Pack[49].score = 10;
	Pack[50].score = 10;
	Pack[51].score = 10;
}

void displayPack()
{
	for (int loop = 0; loop < NUMBER_OF_CARDS; loop++)
	{
		cout << "Suit = " << Pack[loop].suit;
		cout << "Score = " << Pack[loop].score;
		cout << "Value = " << Pack[loop].value;
		cout << endl;
	}
}

void ShufflePack()
{

	srand(time(0));


	for (int shuffle = 0; shuffle < 100; shuffle++)
	{
		int rndNum1 = rand() % 51;
		int rndNum2 = rand() % 51;

	}


}
closed account (E0p9LyTq)
Using C++11 constructs to create a 52 card deck, display it, shuffle it, and redisplay the shuffled deck.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <vector>
#include <iostream>
#include <numeric> // std::iota
#include <random>  // random engine
#include <chrono>  // system clock

std::default_random_engine& urng();
void randomize();
template<typename T> void display_deck(std::vector<T> const&);

int main()
{
   // randomize the C++11 random engine
   randomize();

   // create an alias:
   using card = unsigned short;

   // manufacture a deck of 52 cards.....
   const card NUM_CARDS = 52;

   // create a std:vector deck of cards:
   std::vector<card> deck(NUM_CARDS);

   // "populate" the deck, from zero to 51:
   std::iota(deck.begin(), deck.end(), 0);

   // display the unshuffled deck
   display_deck(deck);
   std::cout << '\n';

   // shuffle the deck:
   std::shuffle(deck.begin(), deck.end(), urng());

   // display the shuffled deck
   display_deck(deck);
}


std::default_random_engine& urng()
{
   // create a random engine that lasts the entire lifetime of the program
   static std::default_random_engine URNG { };
   return URNG;
}

void randomize()
{
   urng().seed(static_cast<unsigned> (std::chrono::system_clock::now().time_since_epoch().count()));
}

template<typename T> void display_deck(std::vector<T> const& deck)
{
   // create 2 lambda functions to help display each card in the deck:
   auto rank = [ ](T c) { return "A23456789TJQK"[c % 13]; };
   auto suit = [ ](T c) { return "SHDC"[c / 13]; };

   T count = 0;

   for (const T& c : deck)
   {
      std::cout << rank(c) << suit(c) << ' ';
      count++;

      // have 13 cards been displayed?
      if (0 == (count % 13))
      {
         std::cout << '\n';
      }
   }
}
AS 2S 3S 4S 5S 6S 7S 8S 9S TS JS QS KS
AH 2H 3H 4H 5H 6H 7H 8H 9H TH JH QH KH
AD 2D 3D 4D 5D 6D 7D 8D 9D TD JD QD KD
AC 2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KC

AH 6S 8C JC TH 7H QD 8H 6D 4S 7S QS KD
TD 9C 4C KC JH 8S TS 3H 2H JS QH AD 9H
2S 9D 2D 5S 5C 4D 9S 3D 6H QC JD 3C KS
5D 5H KH 7D TC 7C AC 2C AS 4H 6C 3S 8D

The results of the shuffle will be different each time the program is run.

I know this is a lot to absorb, but can be worth the time to learn how to obtain random numbers in C++.
Topic archived. No new replies allowed.