var
  Form1: TForm1;
  num:array[0..5] of integer;
implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
  i:integer;
  j:integer;
begin
  for i := 0 to 4 do
  begin
    repeat
      for j := 0 to i do
        if num[j]=num[i] then
           num[i]:=random(23);
  until(num[j]<>num[i]);
  end;  edit1.Text:=inttostr(num[0]);
  edit2.Text:=inttostr(num[1]);
  edit3.Text:=inttostr(num[2]);
  edit4.Text:=inttostr(num[3]);
  edit5.Text:=inttostr(num[4]);
end;
这是我的代码,但总有重复的数字出现

解决方案 »

  1.   

    var
      NumList: TList;
      I,iIndex: Integer;begin
      NumList := TList.Create;
      try
        for I := 0 to 23 do
          NumList.Add(Pointer(i));
        for I := 0 to 5 - 1 do begin
          Randomize;
          iIndex := Random(NumList.Count);
          ShowMessage(IntToStr(Integer(NumList.Items[iIndex])));
          NumList.Delete(iIndex);;
        end;
      finally
        NumList.Free;
      end;
    end;
      

  2.   

    var
      NumList: TList;
      I,iIndex: Integer;
      strNum: AnsiString;
    begin
      NumList := TList.Create;
      try
        for I := 0 to 23 do
          NumList.Add(Pointer(i));
        for I := 0 to 5 - 1 do begin
          Randomize;
          iIndex := Random(NumList.Count);
          strNum :=IntToStr(Integer(NumList.Items[iIndex]));
          case I of
            0: Edit1.Text := strNum;
            1: Edit2.Text := strNum;
            2: Edit3.Text := strNum;
            3: Edit4.Text := strNum;
            4: Edit5.Text := strNum;
          end;
          NumList.Delete(iIndex);;
        end;
      finally
        NumList.Free;
      end;
    end;
      

  3.   

    谢谢,不过有几个我不是很明白,1、TList是字符列表还是数字列表?2、关于randomize说是初始化内置的随机数生成器,内置的随机数指的是哪些,忘赐教啊!
      

  4.   

    看到这个代码没有?
    NumList.Add(Pointer(i));
    list可以是字符、数字或者其他的列表类,直接指向指针
    范围在0至RAND_MAX 间
      

  5.   

    RAND_MAX的范围最少是在32767之间(int),即双字节(16位数)。若用unsigned int 双字节是65535,四字节是4294967295的整数范围。
      

  6.   

    TList里面存的是TObject指针,在win32系统当中,可以是任何指针类型或者其它32bit的数据类型,包括如integer,longint,longword等等。