public class BallsBins { public static int [] Bins; public static int bb(int size) { System.out.println("Throwing "+size+" balls into "+size+" bins."); Bins = new int[size]; for(int i = 0; i < Bins.length; i++) Bins[i] = 0; for(int i = 0; i < size; i++) ++Bins[((int)(Math.random()*size))]; int max = 0; for(int i = 0; i < Bins.length; i++) max = (max < Bins[i])? Bins[i] : max; return max; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub if(args.length < 1) { System.out.println("Usage: java balls.BallsBins #balls"); System.out.println("Code will throw #balls into #bins randomly and find the max in a bin."); return; } int size = Integer.parseInt(args[0]); System.out.println("Found at most "+bb(size)+" balls in a bin."); } }