private static int sampleFromDistribution(double[] distribution,Random rng){
  double d=rng.nextDouble();
  double sum=0.0;
  for (int i=0; i < distribution.length; i++) {
    sum+=distribution[i];
    if (d <= sum)     return i;
  }
  throw new IllegalArgumentException("Distribution is invalid? d=" + d + ", sum="+ sum);
}

private static int sampleFromDistribution ( double [ ] distribution , Random rng ) { double d = rng . nextDouble ( ) ; double sum = 0.0 ; for ( int i = 0 ; i < distribution . length ; i ++ ) { sum += distribution [ i ] ; if ( d <= sum ) return i ; } throw new IllegalArgumentException ( "Distribution is invalid? d=" + d + ", sum=" + sum ) ; }
given a probability distribution over discrete classes , sample from the distribution and return the generated class index .
