Get Random Element(s) from a List
问题 I'm basically looking for an Elixir equivalent of Ruby's Array#sample. Something that would let me do this: list = [1,2,3,4,5,6,7] sample(list) #=> 4 sample(list, 3) #=> [6, 2, 5] I didn't find anything in the Elixir List Docs either. 回答1: Updated Answer As José Valim said in his answer, in Elixir 1.1 and above, you can now use these methods to get random element(s) from a list: Enum.random/1 - For getting single random element Enum.take_random/2 - For getting multiple random elements Example