title: Rewriting numpy.random.binomial using a For loop
body: <p>Currently I have the following code</p>
  
  <pre><code>def approx_binomial(n, p, size=None):
      gaussian = np.random.normal(n*p, n*p*(1-p), size=size)
      # Add the continuity correction to sample at the midpoint of each integral bin.
      gaussian += 0.5
      if size is not None:
          binomial = gaussian.astype(np.int64)
      else:
          # scalar
          binomial = int(gaussian)
      return binomial
  </code></pre>
  
  <p>However, it is not very accurate as it uses the random function. Is there any other way in which I can rewrite the function using a for loop? </p>
  
  <p>Another query I have is how to display a graph of the probability mass function against the number of successes?</p>
  
  <pre><code>def approx_binomial(n, p, size=None):
      gaussian = np.random.normal(n*p, n*p*(1-p), size=size)
      # Add the continuity correction to sample at the midpoint of each integral bin.
      gaussian += 0.5
      if size is not None:
          binomial = gaussian.astype(np.int64)
      else:
          # scalar
          binomial = int(gaussian)
      return binomial
  plt.plot(n,p)
  plt.show()
  </code></pre>
  
  <p>Thanks!</p>