Results 1 to 3 of 3
  1. #1
    Nazrininator is offline Touhou-Stepmania Fan
    Join Date
    May 2014
    Location
    USA
    Posts
    11
    Rep Power
    0

    Default 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.

    Java 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!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Nazrininator is offline Touhou-Stepmania Fan
    Join Date
    May 2014
    Location
    USA
    Posts
    11
    Rep Power
    0

    Default Re: Random Bingo Card Generator

    All right! Thanks! Now my class is working like it should be!
    Last edited by Nazrininator; 05-26-2014 at 05:44 PM.

Similar Threads

  1. looking for feedback on random card generator
    By Russd772 in forum New To Java
    Replies: 8
    Last Post: 12-28-2012, 06:06 AM
  2. ID Card Generator Help
    By werewolves12 in forum New To Java
    Replies: 1
    Last Post: 01-20-2012, 07:39 AM
  3. Random sentence generator
    By BrAD19924 in forum New To Java
    Replies: 6
    Last Post: 05-03-2011, 03:58 AM
  4. Random Generator
    By Learning Java in forum New To Java
    Replies: 9
    Last Post: 05-31-2010, 09:15 PM
  5. Random Generator
    By padutch2 in forum New To Java
    Replies: 1
    Last Post: 12-03-2007, 07:43 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •