public static int discrete ( double [ ] a ) { if ( a == null ) throw new NullPointerException ( "argument array is null" ) ; double EPSILON = 1E-14 ; double sum = 0.0 ; for ( int i = 0 ; i < a . length ; i ++ ) { if ( ! ( a [ i ] >= 0.0 ) ) throw new IllegalArgumentException ( "array entry " + i + " must be nonnegative: " + a [ i ] ) ; sum = sum + a [ i ] ; } if ( sum > 1.0 + EPSILON || sum < 1.0 - EPSILON ) throw new IllegalArgumentException ( "sum of array entries does not approximately equal 1.0: " + sum ) ; // the for loop may not return a value when both r is (nearly) 1.0 and when the // cumulative sum is less than 1.0 (as a result of floating-point roundoff error) while ( true ) { double r = uniform ( ) ; sum = 0.0 ; for ( int i = 0 ; i < a . length ; i ++ ) { sum = sum + a [ i ] ; if ( sum > r ) return i ; } } }
Returns a random integer from the specified discrete distribution .