我试图将R * C / 2-1唯一数字写入数组,但随机函数给出了相同值的多倍。 它应该在随机位置填充每个数字2次。
像:
( 1 2 3 4 )
( 5 6 7 8 )
( 1 2 3 4 )
( 5 6 7 8 )
到目前为止我的代码:
...
R=4
C=4
...
var
somearray : array [0 .. (C- 1), 0 .. (R- 1)] of integer;
...
for Row := 0 to (R - 1) do
for Col := 0 to (C - 1) do
begin
somearray [Col, Row] := RandomRange(0, 9);
end;
...
EDIT1:
第二个数组
...
var
maxnum: array [0 .. (C * R) div 2 - 1] of integer;
max := (C * R) div 2;
for i := 0 to max - 1 do
maxnum[i] := i;
...
maxnum的数字为0..7 / 1-8
答案 0 :(得分:2)
随机功能不适用于此类用途。
相反:
答案 1 :(得分:2)
我有两种方法可以看到这一点:
迭代数字,然后将每个数字放在随机位置。如果你遇到冲突位置,那么你必须有一些逻辑来找到该值的下一个位置。
使用您的代码我们会得到:
// Initialize the array
for Row := 0 to (R - 1) do
for Col := 0 to (C - 1) do
somearray[Col, Row] := 0;
// Populate the random numbers
for randomCount := 1 to 2 do
for randomNumber := 1 to ((R * C) div 2) do
begin
randomPosition := Random(R * C);
while somearray[randomPosition div C, randomPosition mod C] <> 0 do
begin
Inc(randomPosition);
if randomPosition >= (R * C) then
randomPosition := 0;
end;
somearray[randomPosition div C, randomPosition mod C] := randomNumber;
end;
将其置于更通用的形式:
type
T2DArray = array of array of Integer;
procedure RandomPopulate1(aTheArray: T2DArray);
var
col: Integer;
colCount: Integer;
randomCount: Integer;
randomNumber: Integer;
randomPosition: Integer;
row: Integer;
rowCount: Integer;
begin
// Initialize the array
colCount := Length(aTheArray);
if colCount = 0 then
Exit;
rowCount := Length(aTheArray[0]);
for col := 0 to colCount - 1 do
for row := 0 to rowCount - 1 do
aTheArray[col, row] := 0;
// Populate the random numbers
for randomCount := 1 to 2 do
for randomNumber := 1 to ((colCount * rowCount) div 2) do
begin
randomPosition := Random(colCount * rowCount);
while aTheArray[randomPosition div colCount, randomPosition mod colCount] <> 0 do
begin
Inc(randomPosition);
if randomPosition >= (colCount * rowCount) then
randomPosition := 0;
end;
aTheArray[randomPosition div colCount, randomPosition mod colCount] := randomNumber;
end;
end;
要使用它:
var
somearray: T2DArray;
begin
Randomize;
SetLength(somearray, 4, 4);
RandomPopulate1(somearray);
end;
您可以设置数组中的值,然后随机化位置。因此,执行一些随机交换。这是更容易的选择。
// Initialize the array
for Row := 0 to (R - 1) do
for Col := 0 to (C - 1) do
somearray[Col, Row] := ((Col * C + Row) div 2) + 1;
// Now randomize the positions
for randomLoop := 0 to (R * C) - 1 do
begin
randomPosition := Random(R * C);
randomNumber := somearray[randomLoop div C, randomLoop mod C];
somearray[randomLoop div C, randomLoop mod C] := somearray[randomPosition div C, randomPosition mod C];
somearray[randomPosition div C, randomPosition mod C] := randomNumber;
end;
和以前一样,我们可以有更通用的版本:
procedure RandomPopulate2(aTheArray: T2DArray);
var
col: Integer;
colCount: Integer;
randomLoop: Integer;
randomNumber: Integer;
randomPosition: Integer;
row: Integer;
rowCount: Integer;
begin
// Initialize the array
colCount := Length(aTheArray);
if colCount = 0 then
Exit;
rowCount := Length(aTheArray[0]);
for col := 0 to colCount - 1 do
for row := 0 to rowCount - 1 do
aTheArray[col, row] := ((col * colCount + row) div 2) + 1;
// Now randomize the positions
for randomLoop := 0 to ((colCount * rowCount) div 2) do
begin
randomPosition := Random(colCount * rowCount);
randomNumber := aTheArray[randomLoop div colCount, randomLoop mod colCount];
aTheArray[randomLoop div colCount, randomLoop mod colCount] :=
aTheArray[randomPosition div colCount, randomPosition mod colCount];
aTheArray[randomPosition div colCount, randomPosition mod colCount] := randomNumber;
end;
end;
答案 2 :(得分:0)
您可以尝试以下操作。
创建一个动态数组(或TList<integer>
),其项目数等于2d数组中的单元格数。
使用连续整数填充此数组。例如,将1分配给Item [0],2分配给Item [1]等
生成r:= Random(List.Count)。将List [r]值分配给2d数组成员,并从列表中删除该成员。 这将导致List.Count的减少,即使索引相同,新的r代也会给你一个i)Distinct项值。 ii)随机函数将在主数组的剩余部分内为您提供项索引,直到Length(List)= 0.