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

Andreas Hillqvist andreas.hillqvist@REDACTED
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].

    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]).

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

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)

Andreas Hillqvist

More information about the erlang-questions mailing list