Re: Password Generator
- From: Dr John Stockton <jrs@xxxxxxxxxxxxxxxxxx>
- Date: Fri, 28 Oct 2005 13:27:31 +0100
JRS: In article <4361290c$0$11070$e4fe514c@xxxxxxxxxxxxxx>, dated Thu,
27 Oct 2005 21:22:39, seen in news:comp.lang.pascal.delphi.misc, Maarten
Wiltink <maarten@xxxxxxxxxxxxxxxxxx> posted :
>"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 :).
I can evaluate 26+26+10 and type 62 faster than I can type length(s).
For some string types, length(s) is expensive, and should be done at
most once in the routine; at least in Pascal, it can be done at compile
time.
>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.
Undoubtedly, except for the repeatedly-evaluated (if not optimised away)
length(s).
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
The Big-8 newsgroup management is attempting to legitimise its questionable
practices while retaining its elitist hegemony. Read <URL:news:news.groups>.
.
- Follow-Ups:
- Re: Password Generator
- From: Maarten Wiltink
- Re: Password Generator
- References:
- Password Generator
- From: Ryan Styles
- Re: Password Generator
- From: Charles Appel
- Re: Password Generator
- From: Dr John Stockton
- Re: Password Generator
- From: Nicholas Sherlock
- Re: Password Generator
- From: Maarten Wiltink
- Password Generator
- Prev by Date: Re: dynamic object creation
- Next by Date: delphi.codefetch.com source code search engine launch
- Previous by thread: Re: Password Generator
- Next by thread: Re: Password Generator
- Index(es):