0

Need to get random 2 values from the range 0..9. I tried as,

iex(13)> Enum.take_random(Enum.to_list(0..9),2)
[3, 0]
iex(14)>
nil
iex(15)> Enum.take_random(Enum.to_list(0..9),2)
'\a\b'
Dheena
  • 117
  • 9
  • `'\a\b' == [7, 8]`. Let me know if the linked answer doesn't answer your question. – Dogbert Aug 24 '17 at 09:42
  • Also, you can pass the range directly to `Enum.take_random`. Doing `Enum.to_list()` on it first is unnecessarily inefficient. – Dogbert Aug 24 '17 at 09:42
  • take_random will give only one value, but i need more than one random value – Dheena Aug 24 '17 at 09:56
  • `take_random` returns as many values as you pass as the second argument. You've even posted a code snippet which shows this behavior. Are you expecting a different result than in the code snippet in the question? – Dogbert Aug 24 '17 at 10:59
  • but take_random need list as one input , i need to convert my range to input right? – Dheena Aug 25 '17 at 03:11
  • `take_random` takes any `Enumerable` as input and a range is an `Enumerable`. You can pass the range as it is. `Enum.take_random(1..10, 2)` works. – Dogbert Aug 25 '17 at 05:28
  • `Enum.take_random(1..10, 2) |> to_charlist` will always work :) – arpit Aug 25 '17 at 06:55

0 Answers0