vb.net Getting numbers from array horizontally and vertically?

时间:2016-02-12 22:00:18

标签: vb.net

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

enter image description here 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 

1 个答案:

答案 0 :(得分:0)

根据您的评论回复,我了解该计划必须:

  

随机选取2D数组中的任意六个元素,并且每个元素或值只能选择一次。

这很简单:这是一个伪代码实现:

  1. 执行一些初步计算:
    1. 获取2D数组的边界
    2. 种子随机数生成器:
    3. 创建HashSet实例
  2. 获取六对坐标:
    1. 持续循环,直到HashSet中有6个元素:
      1. 在步骤1中找到的数组范围内生成坐标对。
      2. 从坐标对中获取值
      3. 检查我们之前是否看到了这个值(通过查看HashSet)。如果我们之前没有看过,请将其添加到HashSet并继续,否则请忽略它并重试。
  3. 返回HashSet
  4. 的内容

    在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();
    

    请注意,通过跟踪观察值而不是坐标,我们可以同时避免重复值和重复坐标:因为重复坐标无论如何都会为您提供重复值。