各位,如何生成由用户指定长度的密码字符串,内容为数字,或者是数字和字母的组合?

解决方案 »

  1.   

    function TForm1.GetPwdStr(iLength: Integer): string;
    const
      pStr = 'abcedfghijklmnopqrstuvwxyz1234567890ABCEDFGHIJKLMNOPQRSTUVWXYZ';
    var
      i: Integer;
    begin
      result := '';
      Randomize;
      for i := 0 to iLength - 1 do
        result := result + pStr[Random(42)];
    end;
      

  2.   

    Sorry,算错了,请修改:
      for i := 0 to iLength - 1 do
        result := result + pStr[Random(42)];
    改为
      for i := 0 to iLength - 1 do
        result := result + pStr[Random(61) + 1];
      

  3.   

    呵呵,改为
    const
      pStr = '0123456789ABCEDFGHIJKLMNOPQRSTUVWXYZ';
    var
      i: Integer;
      l: Integer;
    //...
      l := Length(pStr);
      for i := 1 to iLength do
        result := result + pStr[Random(l) + 1];
    //...