public E sample(final Random rand){
  final double total=totalCount();
  if (total <= 0.0) {
    throw new RuntimeException(String.format("Attempting to sample() with totalCount() %.3f\n",total));
  }
  double sum=0.0;
  final double r=rand.nextDouble();
  for (  final Map.Entry<E,Double> entry : entries.entrySet()) {
    final double count=entry.getValue();
    final double frac=count / total;
    sum+=frac;
    if (r < sum) {
      return entry.getKey();
    }
  }
  throw new IllegalStateException("Shoudl've have returned a sample by now....");
}

public E sample ( final Random rand ) { final double total = totalCount ( ) ; if ( total <= 0.0 ) { throw new RuntimeException ( String . format ( "Attempting to sample() with totalCount() %.3f\n" , total ) ) ; } double sum = 0.0 ; final double r = rand . nextDouble ( ) ; for ( final Map . Entry < E , Double > entry : entries . entrySet ( ) ) { final double count = entry . getValue ( ) ; final double frac = count / total ; sum += frac ; if ( r < sum ) { return entry . getKey ( ) ; } } throw new IllegalStateException ( "Shoudl've have returned a sample by now...." ) ; }
will return a sample from the counter , will throw exception if any of the counts are < 0 . 0 or if the totalcount ( ) < = 0 . 0
