Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,278 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,249 people online right now. Registration is fast and FREE... Join Now!




need help on my Card please

 
Reply to this topicStart new topic

need help on my Card please

Dreambeginner
26 Aug, 2008 - 10:06 AM
Post #1

New D.I.C Head
*

Joined: 26 Aug, 2008
Posts: 1

hello everyone,.
here is my bingo program,. but it happened that i had a problem with it its because it generates same numbers i mean sometimes i get two 8 numbers for letter B and sometimes i get three of them same in I or in any of my bingo letters,. could someone point me to the right direction,. on how can i eliminate those same numbers and change them w/ a new one and i also tried using while(Bnum[ctr]==Bnum[ctr+1]) and what i get ArrayIndexOutOfBoundsException

so far this is my working bingo card,. but generating same numbers,. please help rolleyes.gif

CODE
public class Bingo2
{
    public static void main (String [] args)
    {
    int Bnum[] = new int[5];
    int Inum[] = new int[5];
    int Nnum[] = new int[5];
    int Gnum[] = new int[5];
    int Onum[] = new int[5];
    int Bingonum[][] = new int[5][5];
    int ctr, ctr1,fixnum = 0;
    
    for(ctr = 0; ctr <Bnum.length; ctr++)
        {
            Bnum[ctr] = (int)  (Math.random() * 15 + 1);
        }
        
    for(ctr = 0; ctr <Inum.length; ctr++)
        {
            Inum[ctr] =(int)  (Math.random() * 15 + 16);
        }
        
    for(ctr = 0; ctr <Nnum.length; ctr++)
        {
            Nnum[ctr] =(int)  (Math.random() * 15 + 31);
        }
        
    for(ctr = 0; ctr <Gnum.length; ctr++)
        {
            Gnum[ctr] =(int)  (Math.random() * 15 + 45);
        }
        
    for(ctr = 0; ctr <Onum.length; ctr++)
        {
            Onum[ctr] =(int)  (Math.random() * 15 + 61);
        }
    System.out.print(" B I N G O");
    for(ctr = 0; ctr < Bingonum.length; ctr++)
        {
        System.out.print("\n");
        for(ctr1 = 0; ctr1 < Bingonum.length; ctr1++)
        {
          if (ctr1 >= 4)
              {
              System.out.print(" " + Onum[ctr]);
              }
        else if(ctr1 >= 3)
            {
            System.out.print(" " + Gnum[ctr]);
            }
         else if (ctr1 >= 2)
              {
              if(ctr == 2)
                  {
                  System.out.print(" FR " );
                  }
              else
                  {
                  System.out.print(" " + Nnum[ctr]);
                  }
              }
        else if(ctr1 >= 1)
            {
            System.out.print(" " + Inum[ctr]);
            }
        else
            {
            System.out.print(" " + Bnum[ctr]);
            }
        }
        }
                        
    }
}

here is some of examples,. of same numbers generated
Attached File  untitled.bmp ( 60.17k ) Number of downloads: 7

User is offlineProfile CardPM
+Quote Post

nick2price
RE: Need Help On My Card Please
26 Aug, 2008 - 10:29 AM
Post #2

D.I.C Head
**

Joined: 23 Nov, 2007
Posts: 231



Thanked: 6 times
My Contributions
in order to make sure that there are no repeats, all you have to do is search the existing array for the number that is just drawn:


CODE
//get random number
//loop through the array looking for repeat
//get another random number if there is a repeat
//add random number to array


Or you could just use java.util.Collections.shuffle This doesnt allow repetiton.

This post has been edited by nick2price: 26 Aug, 2008 - 10:33 AM
User is offlineProfile CardPM
+Quote Post

pbl
RE: Need Help On My Card Please
26 Aug, 2008 - 02:43 PM
Post #3

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
I don't see why you call Math.random()...

Bingo cards goes from B1 to B5, I6 to I10, ... or something like that ? Or is it bigger

OK let suppose you want 5 B which range from 1 to 25
The easiest way is to put the values in an ArrayList and then retreive them randomly

CODE

ArrayList<Integer> list = new ArrayList<Integer>();
Random ran = new Random();      
// put the values into it
for(int i = 1; i <= 25; i++)
    list.add(new Integer(i));
// remove them randomly
for(int i = 0; i < 5; i++) {
     Integer x = list.remove(ran.nextInt(list.size());
     Bnum[i] = x.getValue();
}


User is online!Profile CardPM
+Quote Post

Gloin
RE: Need Help On My Card Please
27 Aug, 2008 - 04:11 AM
Post #4

On MeD.i.Cation
Group Icon

Joined: 4 Aug, 2008
Posts: 723



Thanked: 47 times
My Contributions
If you have 15 numbers to choose from, just create an array:

int i;
boolean[] Bnum = new boolean[15]; // Your array is currently of int but I think you should use boolean instead

(make sure the array is initialized to false)

then when you draw a number, you do the following:

i = (int)Math.round(Math.random() * 15 + 1)); //Pick a random number
while (Bnum[i]) { //See if it has been picked before the condition would then be true
i = (int)Math.round(Math.random() * 15 + 1)); //Else pick a new number until the condition evaluates false
}

when it does, you just set:

Bnum[i] = true;

Put it inside a for-loop if you wanna pick more numbers.

Having an array like this will also help you remember what numbers were picked during this round of Bingo (if you have one like it for numbers drawn in the game)

This post has been edited by Gloin: 27 Aug, 2008 - 04:16 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 05:17AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month