dimanche 9 avril 2017

Random matrix fill between 1 and 0 in java

I'm trying to fill randomly a two-dimensional int array with just 0's and 1's this is the method i'm using to fill my array

   private void buildMaze(){
           maze=new int[this.mazeSize][this.mazeSize];
       for(int i=0;i<this.mazeSize;i++){
            for(int j=0;j<this.mazeSize;j++){
                this.maze[i][j]=r.nextInt(2)-1;    
            }
        }
}

The problem that I'm facing is that everytime that i run my tests i get something like this:

| -1 ||  0 ||  0  |
|  0 || -1 || -1  |
|  0 || -1 || -1  |

My question here is: How could i fill the array correctly with random values ​​between 0 and 1?




Aucun commentaire:

Enregistrer un commentaire