随机数字可以用random()函数生成,那随机字符串呢?这个怎么生成?请高手指点

解决方案 »

  1.   

    var
      i:Integer;
      mstr:String;
    begin //产生含有ASCII 33--126字符的长度为8的随机字符串,32是空格,126以上不可显示
      Randomize;
      mstr:='';
      for i:=1 to 8 do
        mstr:=mstr+Char(Byte(' ')+1+Byte(Random(125-32)));
      Caption:=mstr;
    end;
      

  2.   

    function RANDOMStr (ALen : integer) : string;
    const Tbl : array [0 .. $1F] of char = (
      '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
      'C', 'D', 'E', 'F', 'G', 'H', 'K', 'M', 'N', 'P', 'Q', 'R',
      'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
    var i, n, k, x, j : integer;
    begin
      SetString (Result, nil, ALen);
      n := ALen * 5; 
      j := 1;
      while n > 0 do begin
        if n > 30 then k := 30 
         else k := n;
        x := RANDOM (1 shl k - 1);
        for i := 1 to k div 5 do begin
          Result [j] := Tbl [x and $1F];
          inc (j);
          x := x ShR 5;
         end;
        dec (n, k)
       end;
    end;