Home > Enterprise >  How do I stop Math.random() from generating duplicates of a number [duplicate]
How do I stop Math.random() from generating duplicates of a number [duplicate]

Time:09-25

So I'm working on a program about Conway's Game of Life and I'm using 2-D arrays to program the code. I'm using a loop and Math.random() to input alive cells on the array grid and I'm running into the problem of math.random() duplicating numbers so some of the cells aren't displayed on the grid. For example, I'm using a 20x20 grid and if I input 400 alive cells it's supposed to fill up the grid but it's not. How do I fix this?

import java.util.Scanner;

public class GameOfLifeTran {

public static void main(String[] args) {

int howMany, counter,rows,cols, aliveCells,day,stopGame = 0;
String[][] cells = new String[20][20];
String[][] cells2 = new String[20][20];
Scanner input = new Scanner(System.in);



aliveCells= 1;
day = 1;

for (rows = 0; rows < cells.length; rows  ){
for(cols = 0; cols< cells[0].length; cols  ){
    cells[rows][cols]=".";
    cells2[rows][cols]=".";
}
System.out.println();
}

System.out.print("How many cells do you want to be alive? ");
    howMany = input.nextInt();
    for (int i = 0;  i < howMany; i  ) {
       rows = (int)(Math.random()*20.0)*1;
       cols = (int)(Math.random()*20.0)*1;
        cells2[rows][cols] = "O";
    }

while (stopGame != -1 && aliveCells > 0){
System.out.printf("**************Generation %d************** \n" ,day);//Displays day

for (rows = 0; rows < cells.length; rows  ) {
    for (cols = 0; cols < cells[0].length; cols  ) {
        cells[rows][cols] = cells2[rows][cols]; //Makes today's grid, tomorrow
        System.out.print(cells[rows][cols]); //Prints today's grid
    }
    System.out.println(); //Goes to next row
}

counter = 0;
    int a, b;//Represents the neighbour's co-ordinates
    
    /*If the neighbouring cell is not off the grid AND if the cell is alive ("O"), then it will increase the counter*/
    for (rows = 0; rows < cells.length; rows  ) {
        for (cols = 0; cols < cells[0].length; cols  ) {
            a = rows-1;
            b = cols;
            if (a >= 0 && a < 20){
                if (cells[a][b].equals("O")){
                    counter  ;
                }
            }
            a = rows   1;
            b = cols;
            if (a >= 0 && a < 20){
                if (cells[a][b].equals("O")){
                    counter  ;
                }
            }
            a = rows;
            b = cols - 1;
            if (b >= 0 && b < 20){
                if (cells[a][b].equals("O")){
                    counter  ;
                }
            }
            a = rows;
            b = cols   1;
            if (b >= 0 && b < 20){
                if (cells[a][b].equals("O")){
                    counter  ;
                }
            } 
            a = rows - 1;
            b = cols - 1;
            if (a >= 0 & b >= 0){
                if (cells[a][b].equals("O")){
                    counter  ;
                }
            }
            a = rows   1;
            b = cols   1;
            if (a < 20 && b < 20){
                if (cells[a][b].equals("O")){
                    counter  ;
                }
            }
            a = rows - 1;
            b = cols   1;
            if (a >= 0 && b < 20){
                if (cells[a][b].equals("O")){
                    counter  ;
                }
            }
            a = rows   1;
            b = cols - 1;
            if (a < 20 && b >= 0){
                if (cells[a][b].equals("O")){
                    counter  ;
                }
            }
            if (cells[rows][cols].equals("O")) {
                if (counter == 2 || counter == 3) {
                    cells2[rows][cols] = "O";
                 }else {
                    cells2[rows][cols] = ".";
                 }
             }else if (cells[rows][cols].equals(".")) {
                 if (counter == 3) {
                     cells2[rows][cols] = "O";
                 }else {
                     cells2[rows][cols]= "." ;
                 }
            counter = 0;//Resets the counter so that the next cell's neighbours can be checked and counted
        }         
    }
}

int numAlive = 0;//Counter for the number of cells alive
for (rows = 0; rows < cells.length; rows   ){      
    for (cols = 0; cols < cells[0].length; cols  ) {    
        if (cells[rows][cols].equals("O")) {
            numAlive   ;
        }
    }
}


aliveCells = numAlive;

if(aliveCells > 0 ){
    System.out.print("Continue on to the next day? Type in any number for YES, or -1 for NO");
    stopGame = input.nextInt();
}else{
    System.out.println("I'm sorry, all of your cells have died");
}
day  ;
}
 System.out.println("Thanks for playing!");

 }
}

The output when I enter 400 is this: Output

And I want the whole grid to be filled since 20x20 is 400 and the user inputted 400 cells to be alive

CodePudding user response:

If your random number corresponds to a cell that’s already been populated, keep trying until you get a vacant cell:

for (int i = 0;  i < howMany; i  ) {
    do {
        rows = (int)(Math.random()*20.0)*1;
        cols = (int)(Math.random()*20.0)*1;
    } while (!".".equals(cells2[rows][cols]));
    cells2[rows][cols] = "O";
}

There are other ways to do this, such as creating a set of available cell coordinates and removing from that set each time a cell is assigned a value. But I’m guessing you have not started using collections and data structures yet.

  • Related