import java.util.*; import java.io.*; public class PhoneHash { public static Scanner kb = new Scanner(System.in); public static void main (String[] args) { int x, hash, largest=0, smallest, numempty=0; int pnum, buckets; int[] hist; // find out how many buckets to test System.out.print("Enter the number of buckets: "); buckets = kb.nextInt(); // build histogram hist = new int[buckets]; System.out.println("Building Histogram\n"); for (x = 0; x < 10000; x++) { pnum = (int)(Math.random() * 8000000 + 2000000); hash = pnum % buckets; hist[hash]++; } // calculate bucket depth smallest = Integer.MAX_VALUE; for ( x = 0; x < buckets; x++ ) { System.out.printf("%d numbers in bucket %d.\n", hist[x], x); if ( hist[x] == 0 ) numempty++; if ( hist[x] > largest ) largest = hist[x]; if ( hist[x] < smallest ) smallest = hist[x]; } System.out.printf("%d Buckets\n", buckets); System.out.printf("Deep bucket is %d entries\n", largest); System.out.printf("Shallow bucket is %d entries\n", smallest); } }