Here's some comments added to the code to explain what's going on. :-)
import java.util.*;
public class RandomNumberPrinter
{
// Constants.
public static final int ARRAY_SIZE = 50;
public static final int MAX_NUMBER = 999;
public static void main( String[] args )
{
// Create an empty set to store the unique random numbers.
LinkedHashSet<Integer> numberSet = new LinkedHashSet<Integer>();
// Create a random number generator.
Random random = new Random();
// While the set is smaller than the required size (50)...
while( numberSet.size() < ARRAY_SIZE )
{
// ...add a random number between 0 and the maximum number (999).
// Note: if a value is added that is already in the set, it will be
// overwritten, so the set will stay the same size.
numberSet.add( random.nextInt( MAX_NUMBER + 1 ) );
}
// Convert the set into an array.
Integer[] numbers = numberSet.toArray( new Integer[ 0 ] );
// Print out the numbers in a 10 x 5 grid.
for( int row = 0; row < 5; ++row )
{
for( int col = 0; col < 10; ++col )
{
int index = ( row * 10 ) + col;
System.out.printf( "%03d ", numbers[ index ] );
}
System.out.println();
}
}
}