var
word:string;
pass:string;
I:integer;
begin
Randomize;
word:='abcdefghijklmnopqrstuvwxyz';
pass:='';
for i:=0 to 8 do
pass:=pass+copy(word,trunc(length(word)*random+1),1);
edit1.text:=pass;
end;
// call Randomize only once at application start.
procedure TForm1.FormCreate(Sender: TObject);
begin
Randomize;
end;
function RandomPwd(PWLen: integer): string;
// set the Table of chars to used in Passwords
const StrTable: string =
'!#$%&/()=?@<>|{[]}\*~+#;:.-_' +
'ABCDEFGHIJKLMabcdefghijklm' +
'0123456789' +
'闹茕鳇? +
'NOPQRSTUVWXYZnopqrstuvwxyz';
var
N, K, X, Y: integer;
begin
// check the maximum of Password-length
if (PWlen > Length(StrTable)) then K := Length(StrTable)-1
else K := PWLen;
SetLength(result, K); // set result-string-length
Y := Length(StrTable); // Table length for inner loop
N := 0; // loop start value
while N < K do begin // loop to create K chars
X := Random(Y) + 1; // get next random char
// check the presence of this char in the result-string
if (pos(StrTable[X], result) = 0) then begin
inc(N); // it's not found
Result[N] := StrTable[X]; // store it now
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
cPwd: string;
begin
// e.g. create a random-Password with 30 chars
cPwd := RandomPwd(30);
// ...
end;