2016-11-05 50 views
-1

如何用唯一的随机数填充2维数组(不同的数字在一行/列)?我怎样才能用唯一的随机数填充2维数组

我已经做了它的一个维数组:

class Program 
{ 
    static void Main(string[] args) 
    { 
     Random r = new Random(); 

     int[] x = new int[10]; 

     for(int i = 0; i < x.Length; i++) 
     { 
      x[i] = r.Next(9); 

      for(int j = 0; j < i; j++) 
      { 
       if(x[i] == x[j]) 
       { 
        i--; 
        break; 
       } 
      } 
     } 

     for(int i = 0; i < x.Length; i++) 
     { 
      Console.WriteLine(x[i]); 
     } 

     Console.ReadKey(); 
    } 
} 

回答

3

既然你不想任何重复,使用Random反复,直到你有每一个是不是最好的方法。从理论上讲,如果随机数发生器不提供所需的值,那么像这样的算法可以长时间运行。

既然你知道你想要什么值,但是想要它们是随机的,那么shuffle算法通常是更好的方法。 一个shuffle算法可以让你生成一个所需数值的数组,然后将它们随机排序以获得它们。

让它在多维数组中工作的最简单方法可能是首先将一维数组中的所有值都打包,然后将数组转换为多维数组。 然而,it is possible推广洗牌算法在多维数组上工作。

在引用的答案中提供了代码如何看起来像的一个例子。

0

这听起来有点像家庭作业。这里的洗牌整数的固定范围(在你的情况下,10×10)到二维整数数组,如int[10,10]的想法:

using System; 
using System.Linq; 
using System.Collections; 

class MainClass { 

    public static void Main (string[] args) { 
    // table dimension (assumes a square) 
    var dim = 10; 
    var table = new int?[dim, dim]; 

    // 100 integers: 0..99 
    var queue = new Queue(Enumerable.Range(0, dim * dim).ToList<int>()); 
    var rng = new Random(); 


    int x = dim/2, y = dim/2; 

    // Acceptable shuffle? As long as the queue has anything in it, try to place the next number 
    while(queue.Count > 0) { 
     x = rng.Next(dim); // still using random, not great! :(
     y = rng.Next(dim); 

     if(table[x,y] == null) 
      table[x,y] = (int)queue.Dequeue(); 
    } 

    // print output so I know I'm not crazy 
    for(var i = 0; i < dim; i++) { 
     Console.Write("Row {0}: [", i); 
     for(var j = 0; j < dim; j++) { 
      Console.Write("{0,4}", table[i,j]); 
     } 
     Console.WriteLine("]"); 
    } 
    } 
} 

输出:

Mono C# compiler version 4.0.4.0 

Row 0: [ 55 45 38 23 88 46 7 89 0 94] 
Row 1: [ 2 92 43 51 58 67 82 90 79 17] 
Row 2: [ 29 64 16 8 50 14 1 25 26 73] 
Row 3: [ 97 37 13 20 4 75 98 80 48 12] 
Row 4: [ 33 27 42 74 95 35 57 53 96 60] 
Row 5: [ 59 86 76 40 6 11 77 49 93 61] 
Row 6: [ 5 72 9 91 68 30 39 69 99 21] 
Row 7: [ 52 31 28 34 3 81 18 62 10 71] 
Row 8: [ 66 24 44 54 56 85 84 22 47 63] 
Row 9: [ 65 36 83 41 15 19 87 78 70 32] 
0

这里是我的投篮在实施MAV的回答:

private Random random = new Random(); 

private void Shuffle(ref int[] array) 
{ 
    int r, temp; 
    for (int i = array.Length - 1; i >= 0; i--) 
    { 
     r = random.Next(i + 1); 
     temp = array[r]; 
     array[r] = array[i]; 
     array[i] = temp; 
    } 
} 

public int[,] GetUniqueArray() 
{ 
    int[,] array = new int[10,10]; 

    int[] temp = Enumerable.Range(0, 100).ToArray(); 
    Shuffle(ref temp); 

    for (int i = 0; i < temp.Length; i++) 
    { 
     array[i/array.GetLength(0), i % array.GetLength(1)] = temp[i]; 
    } 

    return array; 
} 

就像他说,暴力破解的数组的内容是随机的,以及独特的可能会导致问题取决于阵列有多大。如果遇到产生大量冲突的情况,那么它可能会导致程序放慢抓取速度,同时为单个数组索引生成数百个随机数字,只是盲目地寻找尚未使用的数字。

这种方法比较好,因为您从一个已经填充了唯一数字的期望大小的数组开始,从这一点开始,您只是随机化它们的顺序。您可以通过持续运行时间和更少的问题获得理想的结果。

相关问题