Binomial Random Variables | function to calculate nCr i . e . , number of ways to choose r out of n objects ; Since nCr is same as nC ( n - r ) To decrease number of iterations ; function to calculate binomial r . v . probability ; Driver code
< ? php function nCr ( $ n , $ r ) { if ( $ r > $ n / 2 ) $ r = $ n - $ r ; $ answer = 1 ; for ( $ i = 1 ; $ i <= $ r ; $ i ++ ) { $ answer *= ( $ n - $ r + $ i ) ; $ answer /= $ i ; } return $ answer ; } function binomialProbability ( $ n , $ k , $ p ) { return nCr ( $ n , $ k ) * pow ( $ p , $ k ) * pow ( 1 - $ p , $ n - $ k ) ; } $ n = 10 ; $ k = 5 ; $ p = 1.0 / 3 ; $ probability = binomialProbability ( $ n , $ k , $ p ) ; echo " Probability ▁ of ▁ " . $ k ; echo " ▁ heads ▁ when ▁ a ▁ coin ▁ is ▁ tossed ▁ " . $ n ; echo " ▁ times ▁ where ▁ probability ▁ of ▁ " . " each ▁ head ▁ is ▁ " . $ p ; echo " is = " ? >