Created
December 15, 2015 16:51
Revisions
-
mk created this gist
Dec 15, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,24 @@ defmodule Mischer do def deck do suits = ["s","h","g","e"] ranks = ["7","8","9","X","U","O","K","A"] for rank <- ranks, suit <- suits, do: rank <> suit end def shuffle(cards \\ deck) do card_count = Enum.count(cards) Enum.reduce 0..(card_count - 1), cards, fn(i, shuffled_cards) -> swap(shuffled_cards, i, rand_uniform(i, card_count)) end end defp swap(list, i, j) do i_element = Enum.at(list, i) j_element = Enum.at(list, j) list |> List.replace_at(i, j_element) |> List.replace_at(j, i_element) end defp rand_uniform(lo, hi) do :crypto.rand_uniform(lo, hi) end end