How can we create a random list of string?


I need to create a function that can return me something like this

“WM"6 random characters"LT”

The final result will be: WM13N321TLT

But both prefix and suffix can be optional without that also it can create some random string of 6 character

a character can be uppercase and downcase both

For example

Enum.to_list(?A..?Z) ++ Enum.to_list(?a..?z) ++ Enum.to_list(?0..?9) |> Enum.take_random(6)

But if you google a little bit, there’s plenty of solutions of various complexity


What have you tried?


if this is for unit tests (or something with a lot of rules) you can look at randex and stream_data. Otherwise, you can just roll your own using Enum.random/1

1 Like

Also what is it for? There are many ways to do it, but not all are appropriate in all situations.