Re: Password Generator



"Nicholas Sherlock" <n_sherlock@xxxxxxxxxxx> wrote in message
news:djr7ao$nk4$1@xxxxxxxxxxxxxxxxxx
> Dr John Stockton wrote:

<obvious version with magic number>

> Small change:
>
> const S =
> 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
>
> function Password( NumChars: integer): string;
> var CharCount: integer;
> begin
> result := '';
> for CharCount := 1 to NumChars
> do result := result + S[random(length(s))+1];
> end;
>
> This way you can add more characters to your string without having to
> count them up :).

Another small change:

function Password( NumChars: integer): string;
var CharCount: integer;
begin
SetLength(Result, NumChars);
for CharCount := 1 to NumChars
do Result[CharCount] := S[random(length(s))+1];
end;

I have a hunch that this reduces the function from potentially quadratic
to guaranteed linear time.

Groetjes,
Maarten Wiltink


.