2016-11-05 55 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; 
} 

就像他說,暴力破解的數組的內容是隨機的,以及獨特的可能會導致問題取決於陣列有多大。如果遇到產生大量衝突的情況,那麼它可能會導致程序放慢抓取速度,同時爲單個數組索引生成數百個隨機數字,只是盲目地尋找尚未使用的數字。

這種方法比較好,因爲您從一個已經填充了唯一數字的期望大小的數組開始,從這一點開始,您只是隨機化它們的順序。您可以通過持續運行時間和更少的問題獲得理想的結果。

相關問題