如何在0-9之间产生一个给定长度的随机数?若要长度达到20位应该怎样实现?
比如:长度为1 ,随机数为8
      长度为2, 随机数为38
      长度为3 ,随机数为558
      ......
      长度为20 ,随机数为01234567890123456789

解决方案 »

  1.   

    function GetRandom(n:Integer): string;
    begin
      Result:= '';
      Randomize;
      for i:= 1 to n do Result:= Result + IntToStr(Random(10));
    end;
      

  2.   

    呵呵,我来补上你的。
    var
      i,Count,Num : integer;
      S : String;
    begin
      Num := random(9)+1;
      S   := inttostr(Num);
     Count := random(20)+1;
      for i := 1 to Count-1 do
      begin
        S := S + inttostr(random(9)+1);
      end;
     ... //这里再把S转变成你要的数。
     
    end;
      

  3.   

    哦,你要有0的话就把random(9)+1;改为random(10)就ok了。记得给分。呵呵。因为今天是周末了。