如何产生500个随机数

解决方案 »

  1.   


    procedure TForm1.Button1Click(Sender: TObject);
    var
    i:integer;
    begin
      while ListBox1.Items.Count<500 do begin
        Randomize;
        ListBox1.Items.Add(IntToStr(Random(1000)));
      end;
    end;
      

  2.   

    up 用静态数组保存也可以, 不知道LZ要哪种;
      

  3.   

    大家好,我是想要500个不同的2000以内的不同的随机会,我的程序如下:var
    i,j:integer;
    a,b,c:integer;
    begin
      a:=0;
      b:=0;
      c:=0;
      ProgressBar1.Position :=0;
      ProgressBar1.Max:=500;
      while a<500 do begin
        Randomize;
        c:=Random(2000);
         ListBox2.Items.Add(IntToStr(c));
        for j := 0 to ListBox1.Count -1  do
        begin
          if c=StrToInt(ListBox1.Items[j]) then
          begin
            b:=1;
          end;
        end;
        if b=0  then
        begin
          ListBox1.Items.Add(IntToStr(c));
          a:=a+1;
          b:=0;
          c:=0;
          ProgressBar1.Position :=ProgressBar1.Position+1;
        end;
      end;
    end;
    可是循环一次,就不动了,请问大家如何处理
      

  4.   

    var
      iCount : Integer;
      List : TStringList;
      strRandom : String;
    begin
      List := TStringList.Create;  Randomize;  iCount := 0;
      while iCount < 500 do
      begin
        strRandom := IntToStr(Random(2000)); // 这里要做下 int -> str 的转换,会浪费些时间。
        if List.Indexof(strRandom) >= 0 then
          coutinue
        else
        begin
          List.Add(strRandom);
          Inc(iCount);
        end;
      end;
    end;
      

  5.   


    修改后的程序如下: 
    procedure TForm1.Button1Click(Sender: TObject);
    var a,b,c,j:Integer;
    begin
      a:=0;
      j:=0;
      c:=0;
     while a<500 do
     begin
       c:=0;
       //Canvas.TextOut(a,a+1,'ds');
       Randomize;
       b:=Random(2000);
       while j<=ListBox1.Count-1  do
       begin
         if b=StrToInt(ListBox1.Items[j]) then
           begin
             c:=1;
             //j:=0;
             Break;
             end
             else
             j:=j+1;
         end;
         if  c=0   then
         begin
           ListBox1.Items.Add(IntToStr(b));
           ProgressBar1.Position:=ProgressBar1.Position+1;
           a:=a+1;
           j:=0;
           end;
       end;end;
      

  6.   

    var 
    i,j:integer; 
    a,b,c:integer; 
    begin 
      a:=0; 
      b:=0; 
      c:=0; 
      ProgressBar1.Position :=0; 
      ProgressBar1.Max:=500; 
      while a <500 do begin
    b:=0;//关键所在 
        Randomize; 
        c:=Random(2000); 
        ListBox2.Items.Add(IntToStr(c)); 
        for j := 0 to ListBox1.Count -1  do 
        begin 
          if c=StrToInt(ListBox1.Items[j]) then 
          begin 
            b:=1; //当有重复时,b=1,当在结束循环时,需将他初始化为0,不然死循环
          end; 
        end; 
        if b=0  then 
        begin 
          ListBox1.Items.Add(IntToStr(c)); 
          a:=a+1; 
          b:=0; 
          c:=0; 
          ProgressBar1.Position :=ProgressBar1.Position+1; 
        end; 
      end; 
    end;