2014-12-13 54 views
0

我已經環視相當多,但似乎找不到滿意的答案。我想用唯一的數字填寫5x5。在第1欄中,它只能是1-5之間的數字,第2欄中的數字16-30等等,直到第5欄的數字在61-75之間。它是賓果董事會。我已經嘗試了幾件事,但沒有一件能夠工作,我似乎找不到用我的規格填充陣列的方法。我到處看到它只是預填充數組的一些例子,或者代碼是爲我提高的方法。如何用不同的行/列中的唯一隨機數填充數組

我不久前創建了一個程序,它創建一個數字1-75的常規數組,然後用fisher/yates算法對其進行混洗。這是我應該繼續使用還是應該從頭開始? enter code here

static void Main(string[] args) 
{ 
    string choise = ""; 
    while (choise != "q" && choise != "Q") 
    { 

     Console.Clear(); 
     Console.WriteLine("[1] for a random row of numbers! \n[2] to start the game! 
     \n[Q] to quit! \nPress enter after your selection."); 
     choise = Console.ReadLine(); 
     if (choise == "1") 
     { 
      RandomNum.randomTal(); 
     } 

    (another class) 
    static Random rnd = new Random(); 
    public static void Shuffle<T>(T[] array) 
    { 
     Console.Clear(); 

     for (int i = 0; i < array.Length; i++) 
     { 
      int r = i + (int)(rnd.NextDouble() * (array.Length - i)); 
      T t = array[r]; 
      array[r] = array[i]; 
      array[i] = t; 
     } 
    } 

(and another class) 

class RandomNum 
    { 
    public static void randomTal() 
    { 
     int[] sifferArray = Enumerable.Range(1, 56).ToArray(); 
     shuffle.Shuffle(sifferArray); 
     foreach (var item in sifferArray) 
     { 
      Console.WriteLine(item); 

     } 
     Console.WriteLine("Press any key to go back."); 
     Console.ReadKey(); 
    } 
+0

fisher/yates算法應該是完美的。假設你想要16 * 30之間的5 *獨特*隨機數。做'Enumerable.Range(16,15).ToArray()。Shuffle()。拿(5);' – dbc 2014-12-13 00:26:34

+0

很抱歉,我真的不知道這樣做。如果我嘗試在RandomNum類中放置它,那麼在「shuffle」下面會顯示「expected」。而枚舉不適用於我認爲的2D數組。 – halalbin 2014-12-13 00:57:20

回答

0
static void Main(string[] args) 
    { 
     var bingoCard = getBingoCard(); 
     int colNum = 1; 

     foreach (var col in bingoCard) 
     { 
      Console.Write("col" + colNum.ToString() + " "); 
      foreach (var item in col) 
      { 
       Console.Write(item + " "); 
      } 
      Console.WriteLine(); 
      colNum++; 
     } 
     Console.WriteLine(); 

     for (int x = 0; x < 5; x++) 
     { 
      for (int y = 0; y < 5; y++) 
      { 
       Console.Write(bingoCard[y][x].ToString() + (bingoCard[y][x] > 9 ? " " : " ")); 
      } 
      Console.WriteLine(); 
     } 

     Console.ReadLine(); 
    } 



    public static int[][] getBingoCard() 
    { 
     var randGen = new Random(); 
     var bingoCard = new int[][]{ 
     new int[5], 
     new int[5], 
     new int[5], 
     new int[5], 
     new int[5] 
    }; 
     for (int y = 0; y < 5; y++) 
     { 
      for (int x = 0; x < 5; x++) 
      { 
       var possibleNumber = randGen.Next((15 * y) + 1, ((y + 1) * 15)); 
       while (bingoCard[y].Any(num => num == possibleNumber)) 
       { 
        possibleNumber = randGen.Next((15 * y) + 1, ((y + 1) * 15)); 
       } 
       bingoCard[y][x] = possibleNumber; 
      } 
     } 
     return bingoCard; 
    } 
+0

謝謝你們!對此,我真的非常感激!但在這最後一篇文章中,我似乎無法理解.next()中的數學運算。我知道如何用「真正的」數學來計算它。但我不明白它在這裏的工作原理以及數字的作用。你想給初學者解釋一下嗎? – halalbin 2014-12-14 02:30:13

+0

而且此代碼不會在數組中生成唯一的數字。 – halalbin 2014-12-14 19:24:18

+0

你是怎麼意思它不生成唯一的數字?你運行它了嗎?它生成唯一的號碼... – 2014-12-14 19:26:46

0

當我有需要,我使用的是這樣的:

private int[] generatePermutation(int size, int seed) 
    { 
     var permutation = new int[size]; 
     Rnd random = new Rnd(seed); 

     List<int> permList = new List<int>(size); 
     for (int i = 0; i < size; i++) permList.Add(i); 

     for (int i = 0; i < size; i++) 
     { 
      int index = random.Next(0, permList.Count); 
      permutation[i] = permList[index]; 
      permList.RemoveAt(index); 
     } 
     return permutation; 
    } 
0

如果你有一個類BingoBoard對應於細胞的嵌入式二維整型數組,你可以產生像這樣的細胞的隨機模式:

public class BingoBoard 
{ 
    public const int BoardDimension = 5; 

    readonly int[,] board = new int[BoardDimension, BoardDimension]; 

    public BingoBoard() 
    { 
     // In column 1 it can only be a number between 1-15 and in column 2 number 16-30 and so on up until column 5 with a number between 61-75. 
     for (int iRow = 0; iRow < board.GetLength(0); iRow++) 
     { 
      var col = Enumerable.Range(iRow * 3 * board.GetLength(1) + 1, 3 * board.GetLength(1)).Shuffle().Take(board.GetLength(1)).ToArray(); 
      for (int iCol = 0; iCol < board.GetLength(1); iCol++) 
       board[iRow, iCol] = col[iCol]; 
     } 
    } 

    public int[,] Board 
    { 
     get 
     { 
      return board; 
     } 
    } 
} 

public static class RandomProvider 
{ 
    // Adapted from http://csharpindepth.com/Articles/Chapter12/Random.aspx 
    private static int seed = Environment.TickCount; 

    [ThreadStatic] 
    static Random random; 

    public static Random GetThreadRandom() 
    { 
     if (random == null) 
      random = new Random(Interlocked.Increment(ref seed)); 
     return random; 
    } 
} 

public class FisherYatesShuffle 
{ 
    public void ShuffleInPlace<T>(T[] array) 
    { 
     var randomizer = RandomProvider.GetThreadRandom(); 

     // http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm , second version. 
     for (int i = 0; i < array.Length; i++) 
     { 
      // http://msdn.microsoft.com/en-us/library/2dx6wyd4%28v=vs.110%29.aspx 
      var j = randomizer.Next(i, array.Length); // i = inclusive lower bound; array.Length = The exclusive upper bound of the random number returned 
      array.Swap(i, j); 
     } 
    } 
} 

public static class ListExtensions 
{ 
    public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> list) 
    { 
     var scrambled = list.ToArray(); 
     new FisherYatesShuffle().ShuffleInPlace(scrambled); 
     return scrambled; 
    } 

    public static void Swap<T>(this T[] list, int i, int j) 
    { 
     if (list == null) 
      throw new ArgumentNullException(); 
     if (i != j) 
     { 
      T temp = list[i]; 
      list[i] = list[j]; 
      list[j] = temp; 
     } 
    } 
}