简体   繁体   中英

Trying to get non repeating numbers by using nested loop..(2-D array)

I am trying to figure out a way that I can insert random numbers into a 2-d array that is 5x5. I am working on a bingo game so it anyone could help me I would be greatly appreciative. I am fairly new to Java, so any help would be good. Thanks.

Consider:

boolean matchFound;  // used to track when a repeat is found
int rand; // temporarily store the random number before putting it into the array to check if it's a repeat

Random rn = new Random();
for (int i = 0; i < 25 ; i++) {
    rand = rn.nextInt(15) + 1;
    matchFound = false;  // assume that the number generated is not a repeat
    for (int x = 0; x <= 5; x++) {
        for (int j = 0; j <= 5 ; ++j){
            if (rand == b[x][j]) {  // check if the number just generated is a repeat
                matchFound = true;  // if it is, set the boolean to True and use that after the loop
            }           
            if (matchFound == true) { // if the last number is a repeat, then
                i = i - 1;  // reduce the loop counter by 1 to go back to that place for the new number
            } else {
                b[i][j] = rand;  // the number was not repeated so insert it.
            }
        }
    }
}

I would recommend treating it like a bingo game - where you have a set of numbers, randomize them, and the 'pick' numbers for the board. I believe bingo ranges from 1-75? but you could change that. And then just pick the first 25 numbers from your ArrayList, they are sure to be random and not repeated this way.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class Test {
    public static void main(String[] args){
        ArrayList<Integer> bingo = new ArrayList<Integer>();
        int[][] b = new int[5][5];

        // create a list in order
        for (int i = 1; i <= 75; i++) {
          bingo.add(i);
        }

        Collections.shuffle(bingo); // randomize

        // Add to the board
        for (int i = 0; i < 5; i++) {
          for (int j = 0; j < 5; j++) {
            b[i][j] = bingo.remove(0); // remove the first item in the shuffled list
          }
        }

        System.out.print(Arrays.deepToString(b));
    }
}

As in bingo game, You need to track the column number to get the right range boundary of each column (min & max values in below code).

and for each iteration in the outer loop of column values these values will change accordingly

public static void main(String[] args) {

    int[][] b = new int[5][5]; // 5x5 grid

    for (int i = 0; i < 5; i++) { // loop for columns

        int min = i * 15 + 1;
        int max = (i + 1) * 15;

        for (int j = 0; j < 5; j++) { // loop for rows
            int rand;

            do {
                rand = getRandom(min, max); // generate random int between min & max values
            } while (isMatch(rand, b));

            b[j][i] = rand;
        }
    }
    /* print array values */
    for (int[] x : b)
        System.out.println(Arrays.toString(x));

}

/* return true if num is in the array b */
private static boolean isMatch(int num, int b[][]) {
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            if (num == b[i][j]) {
                return true;
            }
        }
    }
    return false;
}

/* return integer number between min & max inclusive */
public static int getRandom(int min, int max) {
    return (int) ((Math.random() * (max + 1 - min)) + min);
}

Output

[14, 22, 37, 57, 62]
[15, 20, 45, 55, 63]
[6, 29, 38, 52, 69]
[11, 30, 34, 59, 74]
[4, 16, 36, 54, 67]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM