Reputation: 13
I have to print a bingo board by using a 2D array and the method I'm using makes logical sense but I can't seem ti figure out why it's still generating same numbers. Any thoughts? I've tried the arraylist but it's a 1D array, if theres a probability it can be a 2D can someone please help. *****Updated, so i used a bit of everything and I'm still jammed.
package bingo.arrays.assignment;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class bingoooooooooooooo {
public static void main(String[] args) {
int[][] card = new int[5][5];
display1(card);
}
public static void display1(int[][] card) {
int[] colm = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
int v = 1, z = 15;
int x = 10000;
List l = new ArrayList();
for (int i : colm) {
l.add(i);
}
Collections.shuffle(l);
//do {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
boolean numberFound = false;
while (!numberFound) {
int number1 = (int) (Math.random() * z) + v;
if (!checkNumber(number1, card, j)) {
break;
}
card[i][j] = number1;
System.out.print("|" + card[i][j]);
}
// if (card[i][j] > 25) {
// x++;
// }
}
v = v + 15;
z = z + 15;
System.out.println(" ");
}
//}while (true);
}
public static boolean checkNumber(int number, int[][] card, int j) {
for(int a = 0; a <= j; a++) {
if(card[i][a] == number) return false;
}
return true;
}
}
Upvotes: 0
Views: 48
Reputation: 347314
Basically, you have overlapping ranges, the number can fall within the range of 0
to z
plus v
. What's happening is a number is been generate in the very low range, but v
isn't large enough to move it beyond the range of the previous set
For example...
z == 130
, v == 13
(0.289 * 130) + 31 = 68
(0.304 * 130) + 31 = 70
(0.934 * 130) + 31 = 152
(0.85 * 130) + 31 = 141
(0.902 * 130) + 31 = 148
z == 145
, v == 13
(0.731 * 145) + 46 = 152
(0.672 * 145) + 46 = 143
(0.016 * 145) + 46 = 48
(0.292 * 145) + 46 = 88
(0.35 * 145) + 46 = 96
You can see that numbers in both sets are overlapping with each other. The problem only gets worse as z
get's larger, as it has more scope to generate overlapping numbers.
Now, I'm sure there's some really awesome mathamtical forumal you could use, but I'm to lazy (and dumb) for that, instead, I'd use a Set
of some kind to generate a random series of 25 numbers. This will ensure that each value is unique
public static void display1(int[][] card) {
// Create the set
Set<Integer> numbers = new HashSet<>(5 * 5);
// Keep looping until we get 25 numbers
while (numbers.size() < 5 * 5) {
// Generate a random number
numbers.add((int)Math.round(Math.random() * 100));
}
// This is just because I'm lazy...
List<Number> listOfNumbers = new ArrayList<>(numbers);
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
// Assign the value to the array...
card[i][j] = (int)listOfNumbers.remove(0);
}
}
// Add just for printing...you could do this in the previous loop
// but I just wanted to keep it clean...
for (int i = 0; i < card.length; i++) {
for (int j = 0; j < card[i].length; j++) {
System.out.printf("|%3d", card[i][j]);
}
System.out.println("|");
}
}
Which will print something like...
| 0| 2| 71| 9| 74|
| 16| 22| 87| 24| 28|
| 93| 30| 33| 40| 41|
| 42| 49| 50| 51| 52|
| 55| 56| 59| 60| 63|
Now, if you aren't to attached to the array, you can use something like...
int index = (row * 5 + col);
to calculate the index
offset into the List
for a given row/col
for (int i = 0; i < card.length; i++) {
for (int j = 0; j < card[i].length; j++) {
int index = (i * 5 + j);
System.out.printf("|%3d", listOfNumbers.get(index));
}
System.out.println("|");
}
Upvotes: 1
Reputation: 101
Your problem seems to be you are only checking the previous number.
You need to loop through all of the numbers in the current section like this:
boolean numberFound = false;
while(!numberFound) {
int number1 = (int) (Math.random() * z) + v;
if(!checkNumber(number1, card, j)) break;
card [i][j]=number1;
System.out.print("|" + card[i][j]);
}
public static boolean checkNumber(int number, int[][] card, int j) {
for(int a = 0; a <= j; a++) {
if(card[i][a] == number) return false;
}
return true;
}
Upvotes: 0