Skip to content

Instantly share code, notes, and snippets.

@tuttlem
Created December 30, 2012 12:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tuttlem/4412555 to your computer and use it in GitHub Desktop.
Save tuttlem/4412555 to your computer and use it in GitHub Desktop.
haskell - cards shuffle
-- | Seeds a list of cards with a random value
seedCards :: StdGen -> [Card] -> [(Card, Int)]
seedCards g [] = []
seedCards g (c:cs) = x:seedCards ng cs
where (seed, ng) = randomR(1, 10000) g :: (Int, StdGen)
x = (c, seed)
-- | Makes an ordered deck of cards
makeDeck :: [Card]
makeDeck = [Card v s | v <- [Ace .. King], s <- [Heart .. Spade]]
-- | Makes a randomly shuffled deck of cards
makeShuffledDeck :: StdGen -> [Card]
makeShuffledDeck g = [x | c <- sorted, let x = fst c]
where cards = seedCards g makeDeck
sorted = sortBy (compare `on` snd) cards
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment