@ Override public double rand ( ) { double np = n * p ; // Poisson approximation for extremely low np if ( np < 1.E-6 ) { return PoissonDistribution . tinyLambdaRand ( np ) ; } boolean inv = false ; // invert if ( p > 0.5 ) { // faster calculation by inversion inv = true ; } if ( np < 55 ) { // inversion method, using chop-down search from 0 if ( p <= 0.5 ) { rng = new ModeSearch ( p ) ; } else { rng = new ModeSearch ( 1.0 - p ) ; // faster calculation by inversion } } else { // ratio of uniforms method if ( p <= 0.5 ) { rng = new Patchwork ( p ) ; } else { rng = new Patchwork ( 1.0 - p ) ; // faster calculation by inversion } } int x = rng . rand ( ) ; if ( inv ) { x = n - x ; // undo inversion } return x ; }
This function generates a random variate with the binomial distribution .