Note 13.6.1.
If you are even the slightest bit unsure on how to begin coding your program, pseudocode is a great place to start!
for (size_t i = 0; i < cards.size(); i++) {
// choose a random number between i and cards.size()
// swap the ith card and the randomly-chosen card
}
randomInt
, which chooses a random integer between the parameters low
and high
, and swapCards
which takes two indices and switches the cards at the indicated positions.
randomInt
by looking at Section 10.7, although you will have to be careful about possibly generating indices that are out of range.
swapCards
yourself. I will leave the remaining implementation of these functions as an exercise to the reader.
randomInt
and swapCards
functions in the commented sections of this active code. Once you’re done with randomInt
and swapCards
, try using them to implement the Deck
member function shuffleDeck
. If done correctly, the program should output a shuffled deck of cards. If you stuck, you can check the hints below.