[erlang-questions] Password generator in Erlang

Zabrane Mickael <>
Tue Aug 14 17:35:24 CEST 2012


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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120814/57ade59a/attachment.html>


More information about the erlang-questions mailing list