      FUNCTION MCBIN(N,P) ! return random integer 0 ... N 
*     from Binomial distribution (N,p) 
      PP=MAX(P,1.0-P) ! P is probability
      K=0
      DO I=1,N        ! N is number of trials
       IF(RANU().LT.PP) K=K+1  
      END DO 
      MCBIN=K
      IF(P.LT.0.5) MCBIN=N-K
      END
