This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
It's not iterating past the end of the loop. It's taking the value in each element (which can be as high as 6, depending on the random number chosen) and using it as an index (which can only take 0-2). So there's a fair chance one of the indexes is going to be out of bounds.
What you need, to give you the equivalent of the 'normal' for loop, is simply:
One thing to remember about the enhance loop is that you don't have access to the index, only the value held in the current position of the array.
Actually you are making a mistake in printing out the elements of the array loc.You have written System.out.println(loc[cell]); where cell refers to values in array loc,and cell can have value greater than 2 which is outside the bounds of array loc.You have to use System.out.println(cell); for printing out array loc elements.