str里包含1-9、a-z这些字符 我想通过一个循环取出n个字符,在循环里通过一个系统随机数来取相对应的字符,例如:
随机数是2,取的结果是2;随机数是3,取的结果是3;随机数是10,取的结果是a;随机数是35,取的结果是z;并且把随机取的n个字符,赋值给str,最后把str赋值给edit1.text例如n为5,取出的字符为adegx。
最好是用数组,请问应该怎么做!
谢谢!!

解决方案 »

  1.   

    function GetFoolStr(n: Integer): string;
    const
      STR = '123456789......z';
    var
      p: Integer;
    begin
      Result := '';
      Randomize;
      for i := 0 to n do
      begin
        p := Random(Length(STR - 1));
        Result := Result + STR[p];
      end;
    end;
      

  2.   

    for i := 0 to n-1 do
    上面写错了
      

  3.   

    p := Random(Length(STR - 1));
    这是什么意思!
      

  4.   

    p := Random(Length(STR - 1));
    取0到str长度之间的一个随机数。
      

  5.   

    function GetFoolStr(n: Integer): string;
    const
      STR = '123456789......z';
    var
      p,i: Integer;
    begin
      Result := '';
      Randomize;
      for i := 0 to n-1 do
      begin
        p := Random(Length(STR) - 1);
        Result := Result + STR[p];
      end;
    end;
      

  6.   

    function GetFoolStr(n: Integer): string;
    const
      STR = '123456789abcdefghijklmnopqrstuvwxyz';
    var
      p,i: Integer;
    begin
      Result := '';
      Randomize;
      for i := 0 to n-1 do
      begin
        p := Random(Length(STR) - 1);
        Result := Result + STR[p];
      end;
    end;
    procedure TForm1.Button1Click(Sender: TObject);
    begin
       memo1.Text:=GetFoolStr(10);
    end;
    为什么有时出现的字符串不是10个?
      

  7.   

    // 改一改,就是了。
    function GetFoolStr(n: Integer): string;
    const
      STR = '123456789abcdefghijklmnopqrstuvwxyz';
    var
      p,i: Integer;
    begin
      Result := '';
      Randomize;
      for i := 0 to n-1 do
      begin
        p := Random(Length(STR) + 1);
        Result := Result + STR[p + 1];
      end;
    end;
    procedure TForm1.Button1Click(Sender: TObject);
    begin
       memo1.Text:=GetFoolStr(10);
    end;
      

  8.   

    说明:
      Random(n): 返回 x 属于 [0, n)  而字符串的弟一个字符的序号为 1 , 而不是 0。
    eg: str := 'abcd';
        str[1] := 'a';
      

  9.   

    这改成这样:   
        p := Random(Length(STR));
        Result := Result + STR[p + 1];