来自:Hexi 时间:99-12-2 21:05:03 ID:163989
procedure RandomX(From,to,number:Integer;N:array of Integer);
var
Temp:Integer;
begin
if Number>(From-To) then Raise Exception.Create('无法生成。');
SetLength(N, Number);
N[0]:=-1;
for i:=1 to Number do
begin
Exist:=False;
repeat
Temp:=Random(To-From)+From;
for j:=0 to i-1 do if Temp=N[j] then
begin
Exist:=True;
Break;
end;
until not Exist;
N[i-1]:=Temp;
end;
end;
调用:
var
Num:array of Integer;
begin
Randomize;
RandomX(200,400,20,N);
.....
SetLength(N,0);
end;
--------------------------------------------------------------------------------
来自:Hexi 时间:99-12-2 21:06:17 ID:163991
更正:
var
Num:array of Integer;
begin
Randomize;
RandomX(200,400,20,Num);
.....
SetLength(Num,0);
end;
--------------------------------------------------------------------------------
来自:jiangtao 时间:99-12-2 21:08:20 ID:163993
用我的这个函数改一下,
//对0~Count的取随机array,任意两个不重复
procedure RandomIntArray(Count:Integer;PInt
IntArray);
var
boolArray
IntArray;
I,K,ti,tk:Integer;
begin
Randomize;
//GetMem(result,Count*sizeof(Integer));
//set array to False
GetMem(boolArray,Count*sizeof(Integer));
FillChar(boolArray^,sizeof(Integer)*Count,0);
for I := 0 to Count-1 do
begin
//randow through 0-thum-1
ti:=Random(Count-i);
tk:=0;
for k:=0 to Count-1 do
begin
if BoolArray^[k] = 0 then
Inc(tk);
if tk-1=ti then
break;
end;
BoolArray^[k]:=1;
PInt^
:=k;
end;
FreeMem(BoolArray);
end;
看看我的,呵呵,正好前几天写了:procedure RandomX(From,to,number:Integer;N:array of Integer);var
iSite:array of integer
iPos:integer;begin
if Number>(From-To) then Raise Exception.Create('无法生成。');
if To < From then Raise Exception.Create('无法生成。');
SetLength(iSite, To - From + 1)
SetLength(N,Number);
for i:=From to To do iSite[i-From] := i;
for i := 1 to Number do begin iPos := random(Length(iSite));
if iPos = Length(iSite) then iPos := 0;
N[i-1] := iSite[iPos]
iSite[iPos] := iSite[High(iSite)];
SetLength(iSite,Length(iSite) - 1)
end;end;
恶贴
核心算法都有了,呵呵,