If and only if you use a Range as input for Enum.random/1 you will get get your result in constant time.
But you also have to remember that there are differences between both versions of code:
def pick20a(enum) do
1..20
|> Enum.map(fn (_) -> Enum.random(enum) end)
end
def pick20b(enum) do
enum
|> Enum.shuffle()
|> Enum.take(20)
end
pick20a/1 might return some elements multipletimes, while pick20b/1 won’t, also pick20b will never return more elements than your input has:
iex(2)> M.pick20a(1..10)
[3, 4, 3, 10, 5, 10, 9, 1, 8, 9, 1, 10, 3, 7, 3, 8, 4, 5, 5, 7]
iex(3)> M.pick20b(1..10)
[7, 3, 5, 1, 6, 2, 9, 8, 4, 10]






















