二维数组唯一编号赋值
Unique number assignment to a two dimensional array
我正在尝试将 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;
...
编辑 1:
第二个数组
...
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
随机函数不是为此类用途设计的。
改为:
- 创建所需值的列表(数组),大概是 1 .. R*C/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;
您可以尝试以下方法。
创建一个动态数组(或 TList<integer>
),其中的项目数等于二维数组中的单元格数。
用顺序整数填充此数组。例如,将 1 分配给 Item [0],将 2 分配给 Item[1] 等
生成一个r:=Random(List.Count)。将 List[r] 值分配给您的二维数组成员并从列表中删除该成员。
这将导致 List.Count 的减少,并且新的 r 代将为您提供 i) 不同的项目值,即使索引相同。 ii) 随机函数将在主数组的剩余部分内为您提供一个项目索引,直到 Length(List)=0.
我正在尝试将 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;
...
编辑 1:
第二个数组
...
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
随机函数不是为此类用途设计的。
改为:
- 创建所需值的列表(数组),大概是 1 .. R*C/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;
您可以尝试以下方法。
创建一个动态数组(或
TList<integer>
),其中的项目数等于二维数组中的单元格数。用顺序整数填充此数组。例如,将 1 分配给 Item [0],将 2 分配给 Item[1] 等
生成一个r:=Random(List.Count)。将 List[r] 值分配给您的二维数组成员并从列表中删除该成员。 这将导致 List.Count 的减少,并且新的 r 代将为您提供 i) 不同的项目值,即使索引相同。 ii) 随机函数将在主数组的剩余部分内为您提供一个项目索引,直到 Length(List)=0.