[erlang-questions] Proposal: Addition to lists module - shuffle/randomize list

Andreas Hillqvist <>
Thu Jan 31 16:30:02 CET 2008

```Is their any interest of a shuffle/randomize function in the list module?

Something like:
shuffle(List0) ->
List1 = [{random:uniform(), X} || X <- List0],
List2 = lists:keysort(1, List1),
[X || {_, X} <- List2].

And:
shuffle(List0, Fun, Seed0) ->
{List1, Seed1} = random_key(List0, Fun, Seed0, []),
List2 = lists:keysort(1, List1),
{[X || {_, X} <- List2], Seed1}.

random_key([], _Fun, Seed0, Acc) ->
{Acc, Seed0};
random_key([X | List], Fun, Seed0, Acc) ->
{Random, Seed1} = Fun(Seed0),
random_key(List, Fun, Seed1, [{Random, X} | Acc]).

And would be called like:
> lists:shuffle([1,2,3,4,5,6,7,8,9,19]).
[1,6,2,19,5,7,9,3,8,4]

> shuffle_list:shuffle([1,2,3,4,5,6,7,8,9,19], fun
random:uniform_s/1, random:seed()).
{[19,2,5,3,7,9,8,6,1,4],{28186,30068,19371}}

I do not believe that it is a "must have function", just a suggestion.

I propose a fun for the random function, to able to provide a other
pseudo-random number generator (PRNG) then the random module.
For example a cryptographically secure pseudo-random number generator (CSPRNG)

Regards
Andreas Hillqvist

```