简体   繁体   English

对如何为数组中的每列生成唯一随机数感到困惑

[英]Confused on how to generate unique random numbers for each column in an array

I'm writing a program to create a bingo game for a class assignment, however I'm a little stuck on how to have a different numbers for each column in the 5x5 array. 我正在编写一个程序来为类分配创建一个宾果游戏,但是我对于如何为5x5数组中的每个列设置不同的数字有点困惑。 The code I have now is creating random numbers for the array, however some runs have the same numbers in a column. 我现在的代码是为数组创建随机数,但是有些运行在列中具有相同的数字。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Below is some coding for the first two columns. 下面是前两列的一些编码。

public static void newCard() {
        System.out.println("B" + "  " + "I" + "  " + "N" + "  " + "G" 
            + "  " + "O");
        int card [][] = new int[5][5];
        for (int i = 0; i < card.length; i++) {
            for (int j = 0; j < card[i].length; j++) { 
                    card[i][0] = (int)(Math.random() * 15 + 1);
                     if (card[i][0] == card[i][j]) {
                    card[i][0] = (int)(Math.random() * 15 + 1);
            card[i][1] = (int)(16 + Math.random() * 15);
                 if (card[i][1] == card[i][j]) {
                card[i][1] = (int)(16 + Math.random() * 15);
                    }
//New Code 
public static void main(String[] args) {
        int[][] card = newCard();
        System.out.println("B  I  N  G  O");
        for (int i = 0; i < card.length; i++) {
            for (int j = 0; j < card[i].length; j++) {
                System.out.printf("%2d ", card[j][i]);
            }
            System.out.println();
        }
        Scanner input = new Scanner(System.in);
        while (calledCard()) {
        System.out.println("Enter the called number: ");
        int number = input.nextInt();
        }
    }
public void calledCard(int number) {
        for(int i = 0; i <= 4; i++) {
            for(int j = 0; j <= 4; j++) {
                if(newCard()[i][j]==number) {
                    newCard()[i][j] = 0;
                }  else  {
                    continue;
                }
            }
        }

    }
The results I get are allowing each column to have some of the same numbers shown.

INFO: 信息:

A typical Bingo game utilizes the numbers 1 through 75. The five columns of the card are labeled 'B', 'I', 'N', 'G', and 'O' from left to right. 典型的宾果游戏使用数字1到75.卡的五列从左到右标记为“B”,“I”,“N”,“G”和“O”。 The center space is usually marked "Free" or "Free Space", and is considered automatically filled. 中心空间通常标记为“自由”或“自由空间”,并被视为自动填充。 The range of printed numbers that can appear on the card is normally restricted by column, with the 'B' column only containing numbers between 1 and 15 inclusive, the 'I' column containing only 16 through 30, 'N' containing 31 through 45, 'G' containing 46 through 60, and 'O' containing 61 through 75. 可以在卡上显示的打印数字范围通常受列限制,“B”列仅包含1到15之间的数字,“I”列仅包含16到30,“N”包含31到45 ,'G'包含46到60,'O'包含61到75。

you can use Collections.shuffle to randomize each List, here is how to use it: 您可以使用Collections.shuffle随机化每个List,以下是如何使用它:

     public static void newCard() {
        List<List<Integer>> nums = new ArrayList<>();
        int k=1;
        for (int i = 0; i <5; i++) {
            nums.add(new ArrayList<>());
            for(int j=0; j<15; j++){
                nums.get(i).add(k++);
            }
//            System.out.println(nums.get(i));
        }


        System.out.println("B" + "  " + "I" + "  " + "N" + "  " + "G"
                + "  " + "O");
        int card [][] = new int[5][5];
        for (int i = 0; i < card.length; i++) {
            Collections.shuffle(nums.get(i));
        }

        for (int i = 0; i < card.length; i++) {
            for (int j = 0; j < card[i].length; j++) {
                card[i][j] = nums.get(j).get(i);
                //System.out.print(card[i][j]+" ");
            }
            //System.out.println("");
        }
//        System.out.println(card);
    }

Since there is a valid range of values for each column (1-15 for B, 16-30 for I, 31-45 for N, 46-60 for G, and 61-75 for O), you can create a list containing the valid range, randomly shuffle it, and then pick the first five values from the list to fill the bingo card column. 由于每列有一个有效的值范围(B为1-15,I为16-30,N为31-45,G为46-60,O为61-75),您可以创建一个包含有效范围,随机洗牌,然后从列表中选择前五个值以填充宾果卡列。

Here is code to generate an array with values on the range [begin, end]: 以下是生成数组的代码,其范围为[begin,end]:

public static int[] rangeClosed(int beginInclusive, int endInclusive) {
    if (beginInclusive > endInclusive) {
        throw new IllegalArgumentException("Begin must be less than or equal to end for range.");
    }
    int[] range = new int[endInclusive - beginInclusive + 1];
    for (int i = 0; i < range.length; i++) {
        range[i] = beginInclusive + i;
    }
    return range;
}

Here is code to swap two values in an array: 以下是交换数组中两个值的代码:

public static void swap(int[] array, int index1, int index2) {
    if (index1 < 0 || index1 >= array.length) {
        throw new IllegalArgumentException("Swap index one is out of bounds for the given array.");
    } else if (index2 < 0 || index2 >= array.length) {
        throw new IllegalArgumentException("Swap index two is out of bounds for the given array.");
    }
    int tmp = array[index1];
    array[index1] = array[index2];
    array[index2] = tmp;
}

Here is code to randomly shuffle an array by iterating through the array and randomly swapping each index with another: 下面是通过遍历数组并随机地将每个索引与另一个索引交换来随机混乱数组的代码:

public static void shuffle(int[] array) {
    Random rand = new Random();
    for (int i = 0; i < array.length; i++) {
        swap(array, i, rand.nextInt(array.length));
    }
}

Finally, here is the code to randomly generate the bingo card along with a main function to display it: 最后,这里是随机生成宾果卡的代码以及显示它的主函数:

public static int[][] randomBingoCard() {
    int bingoRange = 15;
    int bingoCardSize = 5;
    int[][] card = new int[bingoCardSize][bingoCardSize];
    for (int i = 0; i < card.length; i++) {
        int[] columnRange = rangeClosed(i * bingoRange + 1, i * bingoRange + bingoRange); 
        shuffle(columnRange);
        for (int j = 0; j < card[i].length; j++) {
            card[i][j] = columnRange[j];
        }
    }
    return card;
}

public static void main(String[] args) {
    int[][] card = randomBingoCard();
    System.out.println("B  I  N  G  O");
    for (int i = 0; i < card.length; i++) {
        for (int j = 0; j < card[i].length; j++) {
            System.out.printf("%2d ", card[j][i]);
        }
        System.out.println();
    }
}

EDIT: 编辑:

For your new question to fill in the card, I have included 3 new functions along with an updated main. 为了填写卡片的新问题,我已经包含了3个新功能以及更新的主要功能。 One function to govern the loop by checking to see if all the numbers have been called, one to mark a number when called, and one function to print out the current card. 一个函数通过检查所有数字是否已被调用来控制循环,一个用于在调用时标记数字,一个函数用于打印当前卡。

public static boolean allNumbersMarked(int[][] card) {
    for(int i = 0; i < card.length; i++) {
        for(int j = 0; j < card[i].length; j++) {
            if(card[i][j] != 0) {
                return false;
            }
        }
    }
    return true;
}

public static void markNumber(int[][] card, int number) {
    for(int i = 0; i < card.length; i++) {
        for(int j = 0; j < card[i].length; j++) {
            if(card[i][j] == number) {
                card[i][j] = 0;
            }
        }
    }
}

public static void printCard(int[][] card) {
    System.out.println("B  I  N  G  O");
    for (int i = 0; i < card.length; i++) {
        for (int j = 0; j < card[i].length; j++) {
            System.out.printf("%2d ", card[j][i]);
        }
        System.out.println();
    }
}

public static void main(String[] args) {
    int[][] card = randomBingoCard();
    printCard(card);
    Scanner input = new Scanner(System.in);
    while (!allNumbersCalled(card)) {
        System.out.println("Enter the called number: ");
        int number = input.nextInt();
        markNumber(card, number);
        printCard(card);
    }
    input.close();
}

Due to the new methods that are being added it makes sense to make a BingoCard object that does this for you. 由于添加了新方法,因此制作一个BingoCard对象可以为您完成此操作。 Before when you just wanted a card, a simple static function and main were sufficient, however now it would be worth while to make this program Object Oriented. 在你想要一张卡之前,一个简单的静态函数和main就足够了,但是现在让这个程序面向对象是值得的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM