您通常會使用兩個 for 回圈遍歷 2D 陣列:
For i = 0 to 5
For j = 0 to 5
Console.Writeline(Arr(i,j))
Next
Next
我需要遍歷一個 2D 陣列(具有方形邊緣 - 兩邊的長度相同,即 5x5 7x7 等),但我需要完全隨機地進行,并且不重復相同的元素。
For Each i In iRandArr
For Each j In jRandArr
Console.Writeline(arr(iRandArr(j), jRandArr(i)))
Next
Next
'note: the elements i and j are swapped round above in order to maintain the "column", "row" nomenclature
我已經嘗試通過將每一邊的長度推到一個串列中,將其隨機化(即取消排序,請參見此處),然后以相同的雙回圈方式迭代來實作這一點。不幸的是,這種方法不是隨機迭代,而是通過選擇隨機“列”并隨機迭代“行”來進行半隨機迭代。唯一的問題是迭代受列元素的限制,因此不是真正隨機的(在編程意義上)。
有沒有人可以嘗試任何其他可能的選擇?
uj5u.com熱心網友回復:
這是我評論中該想法的完整編譯實作:
- 構建代表二維陣列中位置的一維點物件集
- 隨機化一維點集
- 迭代隨機集
請注意,使用自定義 Point 結構的選擇是可選的;你可以使用任何你想要的類/結構型別,甚至是Tuple. 隨機化演算法也是如此 - 使用你想要的任何東西。下面的代碼只是問題中鏈接中相同邏輯的一個奇特的 LINQ 實作。
Option Explicit On
Option Strict On
Option Infer Off
Option Compare Binary
Imports System
Imports System.Collections.Generic
Imports System.Linq
Module Module1
Sub Main()
'init an array
' this is just for testing purposes;
' presumably the 'real' array comes from somewhere else
Console.WriteLine("Original Array Ordering:")
Const arrayMaxIndex As Integer = 5
Dim arr(arrayMaxIndex, arrayMaxIndex) As Integer
Dim value As Integer = 0
For row As Integer = 0 To arrayMaxIndex
For col As Integer = 0 To arrayMaxIndex
Console.WriteLine($"Row: {row} | Col: {col} | Value: {value}")
arr(row, col) = value
value = 1
Next
Next
'build a 1D list of points that refer to array locations
' this is written separately from initializing the 2D array on purpose,
' since the assumption is that it will be used on an array of unknown origin
Dim pointList As New List(Of Point)
For row As Integer = 0 To arr.GetUpperBound(0)
For col As Integer = 0 To arr.GetUpperBound(1)
pointList.Add(New Point(row, col))
Next
Next
'randomize the 1D list
' choose whatever randomization algorithm you want; this is just one implementation
Dim rnd As New Random
Dim randomizedList As IEnumerable(Of Point) = pointList.OrderBy(Function() rnd.Next())
'step through the randomized 1D list
Console.WriteLine()
Console.WriteLine("Randomized Array Ordering:")
For Each p As Point In randomizedList
Console.WriteLine($"Row: {p.Row} | Col: {p.Col} | Value: {arr(p.Row, p.Col)}")
Next
End Sub
End Module
'use whatever Point type you want
' Didn't want to hard-code to System.Drawing
Friend Structure Point
Public Sub New(row As Integer, col As Integer)
_Row = row
_Col = col
End Sub
Public ReadOnly Property Row As Integer
Public ReadOnly Property Col As Integer
End Structure
示例輸出(說明沒有重復的“隨機性”):
Original Array Ordering:
Row: 0 | Col: 0 | Value: 0
Row: 0 | Col: 1 | Value: 1
Row: 0 | Col: 2 | Value: 2
Row: 0 | Col: 3 | Value: 3
Row: 0 | Col: 4 | Value: 4
Row: 0 | Col: 5 | Value: 5
Row: 1 | Col: 0 | Value: 6
Row: 1 | Col: 1 | Value: 7
Row: 1 | Col: 2 | Value: 8
Row: 1 | Col: 3 | Value: 9
Row: 1 | Col: 4 | Value: 10
Row: 1 | Col: 5 | Value: 11
Row: 2 | Col: 0 | Value: 12
Row: 2 | Col: 1 | Value: 13
Row: 2 | Col: 2 | Value: 14
Row: 2 | Col: 3 | Value: 15
Row: 2 | Col: 4 | Value: 16
Row: 2 | Col: 5 | Value: 17
Row: 3 | Col: 0 | Value: 18
Row: 3 | Col: 1 | Value: 19
Row: 3 | Col: 2 | Value: 20
Row: 3 | Col: 3 | Value: 21
Row: 3 | Col: 4 | Value: 22
Row: 3 | Col: 5 | Value: 23
Row: 4 | Col: 0 | Value: 24
Row: 4 | Col: 1 | Value: 25
Row: 4 | Col: 2 | Value: 26
Row: 4 | Col: 3 | Value: 27
Row: 4 | Col: 4 | Value: 28
Row: 4 | Col: 5 | Value: 29
Row: 5 | Col: 0 | Value: 30
Row: 5 | Col: 1 | Value: 31
Row: 5 | Col: 2 | Value: 32
Row: 5 | Col: 3 | Value: 33
Row: 5 | Col: 4 | Value: 34
Row: 5 | Col: 5 | Value: 35
Randomized Array Ordering:
Row: 2 | Col: 5 | Value: 17
Row: 3 | Col: 2 | Value: 20
Row: 1 | Col: 0 | Value: 6
Row: 2 | Col: 1 | Value: 13
Row: 4 | Col: 0 | Value: 24
Row: 3 | Col: 5 | Value: 23
Row: 4 | Col: 2 | Value: 26
Row: 5 | Col: 1 | Value: 31
Row: 1 | Col: 5 | Value: 11
Row: 0 | Col: 2 | Value: 2
Row: 4 | Col: 5 | Value: 29
Row: 0 | Col: 5 | Value: 5
Row: 4 | Col: 1 | Value: 25
Row: 3 | Col: 0 | Value: 18
Row: 0 | Col: 4 | Value: 4
Row: 0 | Col: 0 | Value: 0
Row: 1 | Col: 4 | Value: 10
Row: 4 | Col: 3 | Value: 27
Row: 5 | Col: 0 | Value: 30
Row: 2 | Col: 2 | Value: 14
Row: 1 | Col: 1 | Value: 7
Row: 3 | Col: 1 | Value: 19
Row: 2 | Col: 0 | Value: 12
Row: 2 | Col: 4 | Value: 16
Row: 5 | Col: 2 | Value: 32
Row: 0 | Col: 1 | Value: 1
Row: 4 | Col: 4 | Value: 28
Row: 5 | Col: 5 | Value: 35
Row: 1 | Col: 2 | Value: 8
Row: 5 | Col: 4 | Value: 34
Row: 5 | Col: 3 | Value: 33
Row: 1 | Col: 3 | Value: 9
Row: 2 | Col: 3 | Value: 15
Row: 0 | Col: 3 | Value: 3
Row: 3 | Col: 3 | Value: 21
Row: 3 | Col: 4 | Value: 22
PS我希望你不要關心這里的性能;這確實是一個蠻力實作。
uj5u.com熱心網友回復:
這很容易:
Dim Arr As Integer(,) =
{{ 11, 12, 13 }, { 21, 22, 23 }, { 31, 32, 33 }}
Dim random As New Random
Dim indices = _
Enumerable _
.Range(0, 3) _
.SelectMany(Function(i) _
Enumerable _
.Range(0, 3) _
.Select(Function(j) (i, j))) _
.OrderBy(Function(x) random.Next())
For Each x In indices
Console.Writeline(Arr(x.i, x.j))
Next
這只是生成所有可能的索引對,然后隨機打亂它們。這樣可以確保沒有重復。
在這個例子中,我得到:
23
32
22
13
33
12
11
31
21
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/404986.html
標籤:
上一篇:在“forelementin”回圈中的try塊中重復錯誤的迭代-python
下一篇:手風琴ForLoopJS選擇器
