Lesson 29 Adding Sounds. Alternative code for 8 unique random numbers

// declare an empty array
var generatedCards = [Card]()
        
// randomly generate 8 pairs of cards
var generatedNumbers = [Int]()
while generatedCards.count < 16 {
   
   var randomNumber = Int.random(in: 1...13)
   while generatedNumbers.contains(randomNumber) {
      randomNumber = Int.random(in: 1...13)
   }
   generatedNumbers += [randomNumber]
   
   // create two new card objects
   // set image names
   // add them to the array
}

cheers.

Careful… you will end up with 32 cards rather than 16 since each card selected is duplicated. Unless that was intentional on your part.

each successful while loop adds 2 cards to the generatedCards array, so there are only 8 successful while loops.

// add them to the array
generatedCards += [card1, card2]

Ah yes, generatedCards.count rather than generatedNumbers.count

mmm… while generatedNumbers.count < 8 might be easier to understand.