Buggy code:
public class Board { private int boardsize ; private boolean valid = false ; private BoardValues EMPTY = BoardValues . EMPTY ; private BoardValues NORTH = BoardValues . NORTH ; private BoardValues SOUTH = BoardValues . SOUTH ; private BoardValues WEST = BoardValues . WEST ; private BoardValues EAST = BoardValues . EAST ; private BoardValues MIDDLE = BoardValues . MIDDLE ; private ArrayList < ArrayList < BoardValues > > board ; private BoardValues [ ] [ ] boardArrayTest ; private ArrayList < BoardValues [ ] [ ] > boardArray ; private String className = "Board.java" ; private BoardValues [ ] [ ] getBoardArrayTest ( ) { } private Random random = new Random ( ) ; private ArrayList < Ship > shipArray = new ArrayList < Ship > ( Arrays . asList ( new Ship ( 3 , random . nextInt ( 2 ) ) , new Ship ( 3 , random . nextInt ( 2 ) ) , new Ship ( 4 , random . nextInt ( 2 ) ) , new Ship ( 4 , random . nextInt ( 2 ) ) , new Ship ( 5 , random . nextInt ( 2 ) ) ) ) ; public Board ( int boardsize ) { } public ArrayList < ArrayList < BoardValues > > getBoard ( ) { } public BoardValues getValue ( int x , int y ) { } public ArrayList < Ship > getShipArray ( ) { } public void changeBoardValue ( int y , int x , BoardValues value ) { } public void placeShip ( Ship ship , int y , int x ) { } public void randomizeShipPositions ( ) { for ( int i = 0 ; i < ( shipArray . size ( ) ) ; i ++ ) { int x = 0 ; int y = 0 ; while ( ! ( valid ) ) { <START_BUG> x = random . nextInt ( ( ( boardsize ) - ( shipArray . get ( i ) . getShip ( ) . size ( ) ) ) ) ; <END_BUG> y = random . nextInt ( ( ( boardsize ) - ( shipArray . get ( i ) . getShip ( ) . size ( ) ) ) ) ; if ( ( shipArray . get ( i ) . getDirection ( ) ) == 0 ) { int counter = 0 ; for ( int j = y ; j < ( y + ( shipArray . get ( i ) . getShip ( ) . size ( ) ) ) ; j ++ ) { ArrayList < BoardValues > tempRow = board . get ( j ) ; if ( ( tempRow . get ( x ) ) != ( EMPTY ) ) { counter ++ ; } } if ( counter == 0 ) { placeShip ( shipArray . get ( i ) , y , x ) ; valid = true ; } } else if ( ( shipArray . get ( i ) . getDirection ( ) ) == 1 ) { int counter = 0 ; ArrayList < BoardValues > tempRow = board . get ( y ) ; for ( int j = x ; j < ( x + ( shipArray . get ( i ) . getShip ( ) . size ( ) ) ) ; j ++ ) { if ( ( tempRow . get ( j ) ) != ( EMPTY ) ) { counter ++ ; } } if ( counter == 0 ) { placeShip ( shipArray . get ( i ) , y , x ) ; valid = true ; } } } valid = false ; shipArray . get ( i ) . setShipPosition ( x , y ) ; } for ( int i = 0 ; i < ( shipArray . size ( ) ) ; i ++ ) { Log . i ( className , ( ( ( ( ( "<seq2seq4repair_space>public<seq2seq4repair_space>void<seq2seq4repair_space>randomizeShipPositions()<seq2seq4repair_space>-<seq2seq4repair_space>boat<seq2seq4repair_space>" + i ) + "<seq2seq4repair_space>has<seq2seq4repair_space>x<seq2seq4repair_space>=<seq2seq4repair_space>" ) + ( shipArray . get ( i ) . getX ( ) ) ) + "<seq2seq4repair_space>and<seq2seq4repair_space>y<seq2seq4repair_space>=" ) + ( shipArray . get ( i ) . getY ( ) ) ) ) ; } Log . i ( className , ( "\n" + ( board . toString ( ) ) ) ) ; } public int getLength ( ) { } public BoardValues getContentInACell ( int x , int y ) { } public String toString ( ) { } }

Fix:
x = random . nextInt ( ( ( ( boardsize ) - ( shipArray . get ( i ) . getShip ( ) . size ( ) ) ) + 1 ) ) ;