public int createBinomial(final int n,final double p){
  double value=p;
  final StringBuilder bits=new StringBuilder(64);
  double bitValue=0.5d;
  while (value > 0) {
    if (value >= bitValue) {
      bits.append('1');
      value-=bitValue;
    }
 else {
      bits.append('0');
    }
    bitValue/=2;
  }
  final BitString pBits=new BitString(bits.toString());
  int trials=n;
  int totalSuccesses=0;
  int pIndex=pBits.getLength() - 1;
  while (trials > 0 && pIndex >= 0) {
    final BitString bs=new BitString(trials,generator);
    final int successes=bs.countSetBits();
    trials-=successes;
    if (pBits.getBit(pIndex)) {
      totalSuccesses+=successes;
    }
    --pIndex;
  }
  return totalSuccesses;
}

public int createBinomial ( final int n , final double p ) { double value = p ; final StringBuilder bits = new StringBuilder ( 64 ) ; double bitValue = 0.5d ; while ( value > 0 ) { if ( value >= bitValue ) { bits . append ( '1' ) ; value -= bitValue ; } else { bits . append ( '0' ) ; } bitValue /= 2 ; } final BitString pBits = new BitString ( bits . toString ( ) ) ; int trials = n ; int totalSuccesses = 0 ; int pIndex = pBits . getLength ( ) - 1 ; while ( trials > 0 && pIndex >= 0 ) { final BitString bs = new BitString ( trials , generator ) ; final int successes = bs . countSetBits ( ) ; trials -= successes ; if ( pBits . getBit ( pIndex ) ) { totalSuccesses += successes ; } -- pIndex ; } return totalSuccesses ; }
creates a new binomial generator object .
