You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Bingo游戏fillcard()函数数字列范围匹配异常问题求助

Fixing Bingo Card Column Number Mismatch in C#

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:

  1. You’re generating random numbers first, then assigning them to columns arbitrarily (instead of generating numbers for each column within its specific range).
  2. You messed up range boundaries (especially forgetting that Random.Next() is left-closed, right-open—so Next(1,15) only gives 1–14, not 1–15!).
  3. 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 HashSet guarantees 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 have Next(1,15) for B column, that’s wrong—it should be Next(1,16).
  • Verify your range conditions: For example, B column should be num >=1 && num <=15, not num <=16.

This should resolve those pesky misplaced numbers and get your Bingo cards working as expected!

内容的提问来源于stack exchange,提问作者Tanner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:26:20