I have this crazy array.
Type
And I'm trying to link it to a number of conditions and is not well to do I'm trying to try to get 6 index from array
The number can't repeat and it are pick horizontally or vertically randomly
ReDim arrayDeCeldas(filas - 1, columnas - 1)
For i = 0 To filas - 1
For j = 0 To columnas - 1
arrayDeCeldas(i, j) = i & j
Debug.Write(arrayDeCeldas(i, j) & " ")
Next j
Debug.WriteLine("")
Next i
This example about i'm try.
It can be seen, there are a total of 6 items with different sizes
Actually my code, it's getting huge
0 1 2 3 4 5 6 7
10 11 12 13 14 15 16 17
20 21 22 23 24 25 26 27
30 31 32 33 34 35 36 37
40 41 42 43 44 45 46 47
答案 0 :(得分:0)
根据您的评论回复,我了解该计划必须:
随机选取2D数组中的任意六个元素,并且每个元素或值只能选择一次。
这很简单:这是一个伪代码实现:
HashSet
实例HashSet
中有6个元素:
HashSet
)。如果我们之前没有看过,请将其添加到HashSet
并继续,否则请忽略它并重试。HashSet
在C#中,这将是:
Int32[,] numbers = new Int32[] { ... };
Int32 maxY = numbers.GetUpperBound(0); // y-axis in dimension 0
Int32 maxX = numbers.GetUpperBound(1); // x-axis in dimension 1
Random rng = new Random();
HashSet<Int32> values = new HashSet<Int32>();
while( values.Count < 6 ) {
Int32 x = rng.Next( maxX + 1 ); // Random.Next is upperbound exclusive, hence +1
Int32 y = rng.Next( maxY + 1 );
Int32 value = numbers[ y, x ];
if( !values.Contains( value ) ) values.Add( value );
}
return values.ToArray();
请注意,通过跟踪观察值而不是坐标,我们可以同时避免重复值和重复坐标:因为重复坐标无论如何都会为您提供重复值。