Search code examples
randomerlangelixirunique

Create list with random and unique numbers


I'm trying to make a list of unique, random integers of a certain length.

end_list = for x <- 0..10, do: :rand.uniform(50)
> [41, 9, 8, 50, 49, 1, 1, 7, 7, 47, 20]

What can I write to convey an "if not already present" condition in the do: block?


Solution

  • I was offered this one-liner using Stream.repeatedly/1 from somewhere else:

    Stream.repeatedly(fn -> :rand.uniform(50) end) |> Stream.uniq |> Enum.take(10)