[erlang-questions] Password generator in Erlang
Sverker Eriksson
sverker.eriksson@REDACTED
Tue Aug 14 17:43:24 CEST 2012
Try only call random:seed(A1, A2, A3) once at start.
For cryptographic safe random generation look at crypto module.
/Sverker
Zabrane Mickael wrote:
> Hi guys,
>
> This one fires a lot of collisions
> (http://schemecookbook.org/Erlang/NumberRandomNumber):
>
> ------------------
> test() ->
> crypto:start(),
> N = 10000,
> io:format("Generate ~p random passwords and check for collisions ...~n", [N]),
> test(N, dict:new(), 0).
> test(0, _, C) ->
> io:format("Number of collisions: ~p~n", [C]);
> test(N, Dict, C) ->
> Password = passwd(),
> case dict:find(Password, Dict) of
> {ok, _} -> %% collision detected
> test(N - 1, Dict, C + 1);
> error ->
> test(N - 1, dict:append(Password, 1, Dict), C)
> end.
>
> passwd() ->
> passwd(8).
> passwd(Length) ->
> {A1,A2,A3} = now(),
> random:seed(A1, A2, A3),
> lists:flatten(lists:foldl(fun(_,AccIn) ->
> [random:uniform(90) + 32 | AccIn] end,
> [], lists:seq(1,Length))).
>
> ----------------
>
> > passwd:test().
> Generate 10000 random password and check for collisions ...
> Number of collisions: 1182
>
> Anything better guys? Something with less collisions if possible.
>
> Regards,
> Zabrane
>
>
More information about the erlang-questions
mailing list