package main
import (
"fmt"
"math/rand"
"time"
)
/*
A standard deck of 52 playing cards consists of four suits:
spades (♠), hearts (♥), diamonds (♦), and clubs (♣).
Each suit contains 13 ranks:
Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King.
Cards of spades and clubs are black, while hearts and diamonds are red.
Color is informational only — it does not affect shuffling or random selection.
*/
/*
This program picks 5 random cards from a standard 52‑card deck.
It uses:
- []string slices to store ranks, suits, and the deck
- Fisher–Yates shuffle for efficient randomization
- math/rand seeded with time.Now for randomness
Algorithm:
1. Build the deck (52 strings).
2. Shuffle using Fisher–Yates.
3. Print the first 5 cards.
*/
func buildDeck() []string {
ranks := []string{"2","3","4","5","6","7","8","9","10","J","Q","K","A"}
suits := []string{"Clubs", "Diamonds", "Hearts", "Spades"}
deck := make([]string, 0, 52)
// Combine each rank with each suit → 52 unique cards
for _, suit := range suits {
for _, rank := range ranks {
deck = append(deck, rank+" of "+suit)
}
}
return deck
}
func shuffleDeck(deck []string) {
// Fisher–Yates shuffle: efficient and unbiased
for i := len(deck) - 1; i > 0; i-- {
j := rand.Intn(i + 1)
deck[i], deck[j] = deck[j], deck[i]
}
}
func main() {
// Step 1: Build the deck
deck := buildDeck()
// Step 2: Seed the random number generator
rand.Seed(time.Now().UnixNano())
// Step 3: Shuffle the deck
shuffleDeck(deck)
// Step 4: Draw the first 5 cards
fmt.Println("Your 5 random cards:")
for i := 0; i < 5; i++ {
fmt.Println(deck[i])
}
}
/*
run:
Your 5 random cards:
8 of Clubs
A of Hearts
K of Diamonds
2 of Hearts
A of Diamonds
*/