# Random Bingo Card Generator

• 05-26-2014, 05:13 PM
Nazrininator
Random Bingo Card Generator
I'm still pretty new to Java, and I'm trying to create a program that will be able to generate random Bingo cards. This is what I have so far.

Code:

```import java.util.Random; public class BingoCard {   private int[][] card;     public BingoCard(Random random)   {       card = new int[5][5];       for (int c = 0; c < 5; c++)         {             for (int r = 0; r < 5; r++)               card[r][c] = random.nextInt(14) + c * 15 + 1;             checkForDuplicateValues(c, random);         }       card[2][2] = 99; //Free Space   }     public void checkForDuplicateValues(int c, Random random)   {       int yikes = 0;       boolean done = false;       int t = 0;       while (done != true)       {         for (int r = t + 1; r < 5; r++)         {             if (card[r][c] == card[t][c])             {               card[r][c] = random.nextInt(14) + c * 15 + 1;               yikes++;             }             t++;         }         if(yikes == 0)             done = true;         else         {             yikes = 0;             t = 0;         }       }   }     public String toString()   {       String display = "";       for(int r = 0; r < 5; r++)       {         for(int c = 0; c < 5; c++)             display += card[r][c] + " ";         display += "\n";       }       return display;   }     public static void main (String args[])   {       BingoCard[] cards = new BingoCard[30];       Random chance = new Random();       for(int c = 0; c < cards.length; c++)         cards[c] = new BingoCard(chance);       System.out.println(cards[0]);   } }```
The problem with this program is that even with the checkForDuplicateValues method, which checks for duplicate values of a column and replaces the values, it still prints out bingo cards with duplicate values!
• 05-26-2014, 05:16 PM
JosAH
Re: Random Bingo Card Generator
Here's a simple trick: build a Collection (a List will do fine) that contains all possible numbers; use the Collections.shuffle( ... ) method to shuffle the numbers in the List and finally use the first n numbers from that list.

kind regards,

Jos
• 05-26-2014, 05:25 PM
Nazrininator
Re: Random Bingo Card Generator
All right! Thanks! Now my class is working like it should be!