Michael Michael - 1 year ago 92
Java Question

How to display randomly selected strings from an array in java without repetitions

I'm trying to create a Bingo game using NetBeans IDE 6.9.1 (I'm a noob at coding). Right now I'm stuck at trying to create the bingo card. For the 5x5 card's boxes I used jButtons. I can't randomize the "B" bingo balls down the "B" column. I have a code that "works" for randomizing which "B" ball goes in which "B" jButton, but my method will not work for the jLabel I'm using to output the randomly drawn bingo ball. Here's my "random B-ball code:"

String[] Bball1 = {"B5", "B6", "B11"};
String Brandom1 = (Bball1[new Random().nextInt(Bball1.length)]);
String[] Bball2 = {"B1", "B8", "B15"};
String Brandom2 = (Bball2[new Random().nextInt(Bball2.length)]);
String[] Bball3 = {"B3", "B10", "B13"};
String Brandom3 = (Bball3[new Random().nextInt(Bball3.length)]);
String[] Bball4 = {"B2", "B9", "B14"};
String Brandom4 = (Bball4[new Random().nextInt(Bball4.length)]);
String[] Bball5 = {"B4", "B7", "B12"};
String Brandom5 = (Bball5[new Random().nextInt(Bball5.length)]);

Here is the code for when the user clicks the submit button when they pick a bingo pattern and the card would be generated (incomplete):


Yes, this is repetitive, and not overly random, but I did some research on Arrays, as I have not learned them in my Computer Science class, and got this:

public static void main(String[] Bballs) {
String[] Bball;
Bball = new String[2];
Bball[0] = "B1";
Bball[1] = "B2";
Bball[2] = "B3";

int num = (int) (Math.random() * 2);

This is just a test code, and as you can see I could still get B1 more than one time which isn't what I want for the bingo card and the randomly picked bingo balls (which I haven't started yet). Plus, whenever I run my program it doesn't print out the last line. I need this done by the end of Wednesday :/ (It's a late ISU project, and no I did not start it late). Thanks for your time.

Answer Source

I hope this is not too far ahead of where you are, but the easiest way to pull M elements from a bag of N items (balls) without repetition, is to simply add all the elements to a List and shuffle() it, then take the first M values using subList().

Here is a generic method for doing that:

private static List<String> draw(String ballPrefix, int noOfBalls, int noToDraw) {
    List<String> balls = new ArrayList<>();
    for (int ballNo = 1; ballNo <= noOfBalls; ballNo++)
        balls.add(ballPrefix + ballNo);
    return balls.subList(0, noToDraw);


System.out.println(draw("B", 15, 5));

Sample output

[B9, B5, B3, B2, B15]


Ok, in the interest of teaching a bit, a List is somewhat similar to an array, but much more powerful. It contains a sequence of values, like an array, and the values are indexed starting at 0, also like an array.

Unlike an array, where you get the length using arr.length, for a list you call list.size(). Instead of retrieving a value using arr[2], you call list.get(2).

Instead of assigning a value using arr[2] = 7, you can call list.set(2, 7) but that is unusual. This is because, unlike an array that is allocated using new String[5], which allocates a fixed-size array of 5 values, a list is allocated using new ArrayList(), and it will grow in size as needed when calling add(), like the method above does.

After that super short introduction, here is how you use the method above:

List<String> balls = draw("B", 15, 5);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download