I am trying to generate nested random list.
For example
[
[3, 14],
[15, 92]
]
I am able to generate a random list of size n using
n = 5
1..n |> Enum.map(fn _ -> Enum.random(1..100) end)
[41, 43, 3, 94, 25]
When I tried an extended version of the above I got the
same element per row
n = 5
List.duplicate(n, n) |> Enum.map(fn n -> List.duplicate(Enum.random(1..100), n) end)
[
[25, 25, 25, 25, 25],
[3, 3, 3, 3, 3],
[16, 16, 16, 16, 16],
[1, 1, 1, 1, 1],
[18, 18, 18, 18, 18]
]
In ruby I will do this by wrapping the generation of the outer list and the inner in two blocks.
For instance
Array.new(5) { Array.new(5) { rand(1..100) } }
[
[48, 22, 31, 92, 28],
[89, 20, 53, 50, 24],
[52, 10, 7, 44, 55],
[81, 47, 85, 76, 77],
[57, 77, 83, 89, 93]
]
Can I do the same thing in elixir?
Thanks!