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

t ty tty.erlang@REDACTED
Thu Jan 31 16:58:34 CET 2008


Hello,

See also

http://www.erlang.org/pipermail/erlang-questions/2007-June/027158.html

and

http://www.erlang.org/pipermail/erlang-patches/2007-June/000180.html

for previous work.

t

On Jan 31, 2008 10:30 AM, Andreas Hillqvist <andreas.hillqvist@REDACTED> wrote:
> 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
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list