title: Bernoulli random number generator
body: <p>I cannot understand how Bernoulli Random Number generator used in numpy is calculated and would like some explanation on it. For example:</p>
  
  <pre><code>np.random.binomial(size=3, n=1, p= 0.5)
  
  Results:
  [1 0 0]
  </code></pre>
  
  <p>n = number of trails</p>
  
  <p>p = probability of occurrence</p>
  
  <p>size = number of experiments</p>
  
  <p>With how do I determine the generated numbers/results of "0" or "1"?</p>
  
  <p>=================================Update==================================</p>
  
  <p>I created a Restricted Boltzmann Machine which always presents the same results despite being "random" on multiple code executions. The randomize is seeded using
  <code>np.random.seed(10)</code></p>
  
  <pre><code>import numpy as np
  
  np.random.seed(10)
  
  def sigmoid(u):
      return 1/(1+np.exp(-u))
  
  def gibbs_vhv(W, hbias, vbias, x):
      f_s = sigmoid(np.dot(x, W) + hbias)
      h_sample = np.random.binomial(size=f_s.shape, n=1, p=f_s)
  
      f_u = sigmoid(np.dot(h_sample, W.transpose())+vbias)
      v_sample = np.random.binomial(size=f_u.shape, n=1, p=f_u)
      return [f_s, h_sample, f_u, v_sample]
  
  def reconstruction_error(f_u, x):
      cross_entropy = -np.mean(
          np.sum(
              x * np.log(sigmoid(f_u)) + (1 - x) * np.log(1 - sigmoid(f_u)),
              axis=1))
      return cross_entropy
  
  
  X = np.array([[1, 0, 0, 0]])
  
  #Weight to hidden
  W = np.array([[-3.85, 10.14, 1.16],
                [6.69, 2.84, -7.73],
                [1.37, 10.76, -3.98],
                [-6.18, -5.89, 8.29]])
  
  hbias = np.array([1.04, -4.48, 2.50]) #&lt;= 3 bias for 3 neuron in hidden
  vbias = np.array([-6.33, -1.68, -1.25, 3.45]) #&lt;= 4 bias for 4 neuron in input
  
  
  k = 2
  v_sample = X
  for i in range(k):
      [f_s, h_sample, f_u, v_sample] = gibbs_vhv(W, hbias, vbias, v_sample)
      start = v_sample
      if i &lt; 2:
          print('f_s:', f_s)
          print('h_sample:', h_sample)
          print('f_u:', f_u)
          print('v_sample:', v_sample)
      print(v_sample)
      print('iter:', i, ' h:', h_sample, ' x:', v_sample, ' entropy:%.3f'%reconstruction_error(f_u, v_sample))
  </code></pre>
  
  <p><strong>Results:</strong></p>
  
  <pre><code>[[1 0 0 0]]
  f_s: [[ 0.05678618  0.99652957  0.97491304]]
  h_sample: [[0 1 1]]
  f_u: [[ 0.99310473  0.00139984  0.99604968  0.99712837]]
  v_sample: [[1 0 1 1]]
  [[1 0 1 1]]
  iter: 0  h: [[0 1 1]]  x: [[1 0 1 1]]  entropy:1.637
  
  f_s: [[  4.90301318e-04   9.99973278e-01   9.99654440e-01]]
  h_sample: [[0 1 1]]
  f_u: [[ 0.99310473  0.00139984  0.99604968  0.99712837]]
  v_sample: [[1 0 1 1]]
  [[1 0 1 1]]
  iter: 1  h: [[0 1 1]]  x: [[1 0 1 1]]  entropy:1.637
  </code></pre>