function B = d_binornd(N, p, m, n)

% Generate an m x n array of random numbers 
% from the binomial distribution with parameters 
% N (# of trials) and p (probability of success) 
% using the uniform random number generator rand 
% and a direct method of generating  N  uniform random 
% numbers and counting the number less than p.

B = zeros(m, n);

for i = 1:m*n
    u = rand(1,N);
    count = sum(u < p);
    B(i) = count;
end