Aan de slagGa gratis aan de slag

Shuffling a deck of cards

Often times we are interested in randomizing the order of a set of items. Consider a game of cards where you first shuffle the deck of cards or a game of scrabble where the letters are first mixed in a bag. As the final exercise of this section, you will learn another useful function - np.random.shuffle(). This function allows you to randomly shuffle a sequence in place. At the end of this exercise, you will know how to shuffle a deck of cards or any sequence of items.

Examine deck_of_cards in the shell.

Deze oefening maakt deel uit van de cursus

Statistical Simulation in Python

Cursus bekijken

Oefeninstructies

  • Use the np.random.shuffle() function to shuffle deck_of_cards.
  • Select the top three cards from this list by slicing.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# Shuffle the deck
____

# Print out the top three cards
card_choices_after_shuffle = ____
print(card_choices_after_shuffle)
Code bewerken en uitvoeren