Delphi 获取不重复随机数《LceMeaning》

uses
  ShareMem;


type
  TArray = array of Integer;


function CreateRandom(ResultCount, MaxInt: Integer) : TArray;
var                 // 生成个数     最大数值
  I, N, count : Integer;                 
  a : array of Integer;
  label L;
begin
  Randomize;
  SetLength(a,ResultCount);
  for I := 0 to ResultCount -1 do
    begin
      L:
      N := Random(MaxInt);
      for count := 0 to I do
      begin
        if a[count] = N then
        begin
          goto L;
        end;
      end;
      a[i] := N;
      //tmpLst.Add(IntToStr(N));
      Result[i] := n;
    end;
end;


//调用方法
procedure TForm1.btn1Click(Sender: TObject);
var
  MyArr : TArray;
  I : Integer;
  s : string;
begin
  SetLength(MyArr, ResultCount); 
  try
    MyArr := CreateRandom(StrToInt(edt3.Text),lst1.Items.Count);
    for I := 0 to StrToInt(edt3.Text)-1 do
      s := s + lst1.Items[Myarr[i]] + ',';
  except  

  end; 
    Memo2.Text := S;
end;

以上经过XE5编译通过, 如果不引用ShareMem,运行时可能会弹错。

posted @ 2014-04-24 16:30  冰意 LceMeaning  阅读(2101)  评论(0编辑  收藏  举报