Mega Code Archive

 
Categories / Delphi / Strings
 

Belli bir uzunlukta rastgele string uretimi

{ StrTable sabiti string'in icerecegi karakterleri tutar } function RandomString(PWLen: integer): string; const StrTable: string = '!#$%&/()=?@<>|{[]}\*~+#;:.-_' + 'ABCDEFGHIJKLMabcdefghijklm' + '0123456789' + 'NOPQRSTUVWXYZnopqrstuvwxyz'; var N, K, X, Y: integer; begin Randomize; if (PWlen > Length(StrTable)) then K := Length(StrTable)-1 else K := PWLen; SetLength(result, K); Y := Length(StrTable); N := 0; while N < K do begin X := Random(Y) + 1; if (pos(StrTable[X], result) = 0) then begin inc(N); Result[N] := StrTable[X]; end; end; end; // Kullanimi: procedure TForm1.Button1Click(Sender: TObject); begin Label1.Caption := RandomString(30); end;