Standard deck of 52 playing cards
Author scheinerman
2 Stars
Updated Last
1 Year Ago
Started In
July 2020


Cards from a standard deck of fifty two.

Pick a Card

A Card is a standard playing card from a fifty-two card deck. The following functions return a Card:

  • Card(suit,rank) where suit is one of the symbols :clubs, :diamonds, :hearts, or :spades and rank is an integer from 1 (for Ace) to 13 (for King).
  • Card(index) where index is an integer from 1 (for the Ace of Clubs) to 52 (for the King of Spades).
  • Card() returns a random card.
julia> using PlayingCards

julia> Card(:diamonds,12)

julia> Card(49)

julia> Card()

The function string returns a two-character representation of the Card:

julia> string(Card(22))

Alternative input

Cards can be entered via their two-character representation. For example, the nine of diamonds can be entered as 9♢. That is, type 9 then \diamondsuit and then TAB. One may also use \:diamonds:+TAB. Likewise for the other suits.

Face cards, tens, and aces can also be entered in this way (using the capital letters T, J, Q, K, and A followed by the suit symbol):

julia> c = K♠;

julia> println(c)

julia> T♡ == Card(:hearts,10)

These cards can also be entered with a leading 1, 10, 11, 12, or 13:

julia> 10♢

julia> 13♠

julia> 1♣

Note that the four suit symbols are defined as objects of type Suit.

julia> typeof(♠)

A Suit be created either by typing (say) \heartsuit+TAB (giving ♡) or Suit(:hearts)

julia> Suit(:hearts) ==true

Cards can be created either using either a symbol :spades or suit :

julia> a = Card(:spades,5)

julia> b = Card(♠,5)

julia> a==b

Determine Properties

The functions suit and rank return the suit (as a Suit) and the rank (as an Int) of the card.

julia> c = Card()

julia> suit(c)

julia> rank(c)

The function index returns a distinct integer value (from 1 to 52) for the card.

julia> c = Card(17)

julia> index(c)

Use color to determine if the Card is red or black:

julia> C = Card(:clubs,9)

julia> color(C)

julia> color(Card(27))  # this is the Ace of Hearts


Two Cards can be compared with the usual ordering operators (such as <). The ordering of cards is determined first by rank and, if of the same rank, by suit (alphabetically, as in bridge).

julia> Card(:diamonds,10) < Card(:hearts,10)

julia> Card(:diamonds,10) < Card(:clubs,10)

The ace_high function is used to determine if an Ace is higher or lower than the other ranks. Use ace_high(true) or ace_high(false) to set your preference. A call to ace_high() returns the current setting.

julia> ace_high()

julia> Card(:spades,1) < Card(:diamonds,5)

julia> ace_high(false)

julia> Card(:spades,1) < Card(:diamonds,5)


The function deck() returns a 52-long array containing all possible cards in random order. Use deck(false) for them to be returned in a "new box" order.

Use the function print_deck() to see all 52 cards in four lines of thirteen cards each.

julia> d = deck(); print_deck(d)
2♠ J♢ K♠ 3♣ Q♢ 6♢ K♡ 9♣ A♠ 87♡ J♣ 48♣ T♢ T♣ 243♡ T♡ 6♡ Q♣ A♣ 488335♣ J♠ 56♣ A♢ T♠ Q♡ 7976♠
Q♠ J♡ 72♡ A♡ 9245♡ K♣ 95♠ K♢

julia> d = deck(false); print_deck(d)
A♣ 23456789♣ T♣ J♣ Q♣ K♣
A♢ 23456789♢ T♢ J♢ Q♢ K♢
A♡ 23456789♡ T♡ J♡ Q♡ K♡
A♠ 23456789♠ T♠ J♠ Q♠ K♠

Deal a random poker hand like this:

julia> using ShowSet

julia> Set(deck()[1:5])

Long-Form Names

The name function returns a long-form name of a card.

julia> c = Card(:hearts,1)

julia> name(c)
"ace of hearts"


The deck() function returns a randomly shuffled deck of cards with all 52! possible orderings equally likely. However, if one wishes to manually randomize the deck, or to randomize an ordered deck as returned by deck(false), we provide the functions shuffle, riffle, and cut. These functions are typically applied to a full deck of 52 cards, but may be used on any list.


The functions shuffle and shuffle! from the Random module are imported and so may be applied to card decks. These functions apply a random permutation to the cards (all orders equally likely). The function shuffle returns a new deck, leaving the original deck unchanged. The function shuffle! overwrites the deck in the new order.


The functions riffle and riffle! apply a random riffle shuffle to the deck using the Gilbert–Shannon–Reeds model. The former leaves the original deck unchanged and the latter overwrites the deck.

julia> d = deck(false); print_deck(d)
A♣ 23456789♣ T♣ J♣ Q♣ K♣
A♢ 23456789♢ T♢ J♢ Q♢ K♢
A♡ 23456789♡ T♡ J♡ Q♡ K♡
A♠ 23456789♠ T♠ J♠ Q♠ K♠

julia> riffle!(d); print_deck(d)
A♣ 456234789♡ T♡ J♡ Q♡
K♡ A♠ 256789♣ T♣ J♣ Q♣ K♣ A♢
345672384567989♢ T♠ T♢ J♢ Q♢ J♠ K♢ Q♠ A♡ 2♡ K♠ 3

Note that a single riffle shuffle does a poor job at randomizing the deck.


The functions cut and cut! are used to cut the deck.

  • cut!(d,idx) moves cards 1 through idx to the back of the deck; the new first card is the one formerly at position idx+1.
  • cut!(d) cuts the deck a random index. If the deck has n cards, then the cut location is given by the binomial random variable B(n,1/2).
julia> d = deck(false);

julia> cut!(d)

julia> print_deck(d)
Q♢ K♢ A♡ 23456789♡ T♡ J♡
Q♡ K♡ A♠ 23456789♠ T♠ J♠
Q♠ K♠ A♣ 23456789♣ T♣ J♣
Q♣ K♣ A♢ 23456789♢ T♢ J♢

Unicode Characters

The standard playing cards have unicode character representations. Use Char(c) to return that character.

julia> c = Card(:diamonds, 4);

julia> Char(c)
'🃄': Unicode U+1F0C4 (category So: Symbol, other)

The characters are unreadable at small font sizes (e.g., 12 point); they need to be enlarged to be visible.


Developed with input from @charleskawczynski and in parallel with his PlayingCards.jl.

Required Packages

No packages found.

Used By Packages