code: @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;
      }
docs: This function generates a random variate with the binomial distribution.
  
  Uses down/up search from the mode by chop-down technique for n*p &lt; 55,
  and patchwork rejection method for n*p &ge; 55.
  
  For n*p &lt; 1.E-6 numerical inaccuracy is avoided by poisson approximation.