Bingo游戏fillcard()函数数字列范围匹配异常问题求助
Hey there! Let’s dig into why your Bingo card is putting numbers in the wrong columns—this is a super common issue with range logic and random number generation, so we’ll get it sorted out quickly.
First, let’s recap the non-negotiable column rules we need to stick to:
- B column: 1–15
- I column: 16–30
- N column: 31–45
- G column: 46–60
- O column: 61–75
The Likely Culprits in Your fillcard() Function
From what you’ve described, the problem almost always boils down to one of these three issues:
- You’re generating random numbers first, then assigning them to columns arbitrarily (instead of generating numbers for each column within its specific range).
- You messed up range boundaries (especially forgetting that
Random.Next()is left-closed, right-open—soNext(1,15)only gives 1–14, not 1–15!). - Your column assignment logic has incorrect range checks (e.g., checking if a number is ≤16 for B column, which would include 16—belonging to I column).
A Fixed Approach to fillcard()
Instead of generating random numbers across the entire 1–75 range and hoping they land in the right column, let’s generate numbers per column to guarantee they fit the rules. Here’s a cleaned-up example using C#:
using System; using System.Collections.Generic; public class BingoCardGenerator { public int[,] FillCard() { // Define each column's valid number range var columnRules = new Dictionary<char, (int Min, int Max)> { {'B', (1, 15)}, {'I', (16, 30)}, {'N', (31, 45)}, {'G', (46, 60)}, {'O', (61, 75)} }; Random random = new Random(); int[,] bingoCard = new int[5, 5]; // 5 rows x 5 columns (B/I/N/G/O) // Fill each column one at a time for (int colIndex = 0; colIndex < 5; colIndex++) { char columnLetter = "BINGO"[colIndex]; var range = columnRules[columnLetter]; HashSet<int> usedNumbers = new HashSet<int>(); // Avoid duplicate numbers for (int rowIndex = 0; rowIndex < 5; rowIndex++) { // Skip the center cell (N column, 3rd row) for the free space if (colIndex == 2 && rowIndex == 2) { bingoCard[rowIndex, colIndex] = 0; // Use 0 or your own marker for free space continue; } // Generate a unique number within the column's range int randomNum; do { // Important: Add 1 to Max because Random.Next() excludes the upper bound randomNum = random.Next(range.Min, range.Max + 1); } while (!usedNumbers.Add(randomNum)); // Ensure no repeats bingoCard[rowIndex, colIndex] = randomNum; } } return bingoCard; } }
Key Fixes Explained
- Column-specific number generation: We’re only creating numbers that fit the current column’s range, so there’s no way a 24 ends up in B column.
- Correct range handling:
random.Next(range.Min, range.Max + 1)ensures we include the upper bound (e.g., 15 for B column). - Duplicate prevention: The
HashSetguarantees no repeated numbers in a column—standard for valid Bingo cards.
Quick Checks for Your Existing Code
If you want to debug your current fillcard() instead of rewriting:
- Look for where you assign numbers to columns. Are you checking if the number falls within the column’s range before assigning it?
- Double-check your
Random.Next()calls. If you haveNext(1,15)for B column, that’s wrong—it should beNext(1,16). - Verify your range conditions: For example, B column should be
num >=1 && num <=15, notnum <=16.
This should resolve those pesky misplaced numbers and get your Bingo cards working as expected!
内容的提问来源于stack exchange,提问作者Tanner

