Binomial random numbers
R = distfun_binornd(N,pr) R = distfun_binornd(N,pr,[m,n]) R = distfun_binornd(N,pr,m,n)
a matrix of doubles , the total number of binomial trials . N belongs to the set {1,2,3,4,.......}
a matrix of doubles, the probability of getting success in a Bernoulli trial. pr in [0,1].
a 1-by-1 matrix of floating point integers, the number of rows of R
a 1-by-1 matrix of floating point integers, the number of columns of R
a matrix of doubles, the random numbers, in the set {0,1,2,3,...}.
Generates random variables from the Binomial distribution function.
Any scalar input argument is expanded to a matrix of doubles of the same size as the other input arguments.
If N<=2147483647, the algorithm is based on
Kachitvichyanukul, V. and Schmeiser, B. W. "Binomial Random Variate Generation.", Communications of the ACM, 31, 2 (February, 1988) 216.
If N is larger, then we invert the CDF.
// Set the initial seed for tests distfun_seedset(1) // Check with expanded N N = [10 100 1000 10000] pr = 0.1 computed = distfun_binornd(N, pr) expected = [1 19 98 964] // Set the initial seed distfun_seedset(1) // Check with expanded pr N =100 pr = [0.1 0.2 0.3 0.4] computed = distfun_binornd(N, pr) expected = [9 32 29 38] // Check R = distfun_binornd(pr,v) computed = distfun_binornd(100,0.2,[4 5]) //Check mean and variance N = 1000 pr = 0.3 n = 5000 R = distfun_binornd(N,pr,[1 n]); RM = mean(R) RV = variance(R) [M,V] = distfun_binostat(N,pr) // Check actual distribution N=10; pr=0.7; K=10000; R=distfun_binornd(N,pr,1,K); h=scf(); distfun_inthisto(R); h.children.children(1).children.background=-2; x=0:N; y=distfun_binopdf(x,N,pr); plot(x,y,"ro-"); xtitle("Binomial Random Numbers","X","Density") legend(["Empirical","Density"]); | ![]() | ![]() |
http://en.wikipedia.org/wiki/Binomial_distribution