Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to sample from a data set using a binomial distribution with parameters p and n.

Implementation-wise, I follow these steps

  1. I generate an array containing the values of the cumulative distribution function (cdf) for a binomial distribution with parameters p and n:

    cdf[0] = P(X <= 0); cdf[ 1 ] = P(X <= 1); ... cdf[n] = P(X <= n);

  2. I iterate through the data set and for each record in the data set:

    1. I use a Random Number generator to generate a number
    2. I search the position where the generated number would fit in the cdf and take the cdf[i] to the left of the position returned by the search
    3. I sample that record i times because cdf[i] = P(X <= i)

The problem is that I would expect that in ONE run of this algorithm the average of the i's at point 2.3 above (number of times a record is sampled)to be n*p which is the mean of a binomial distribution. Unfortunately it isn't. Is this related to the fact that I run the algorithm only once ? Should I have this kind of expectation only when I run the algorithm a sufficiently large number of times ?

Can you suggest me how I could determine the i's (the number of times a record should be sampled) given that these i's should follow a binomial distribution?

share|cite|improve this question

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.