title: Create matrix with same in and out degree for all nodes
body: <p>I've stated this question in graph theory terms, but that conceptualization isn't necessary.</p>
  
  <p>What I'm trying to do, using Python, is produce a matrix of zeros and ones, where every row has the same number of ones and every column has the same number of ones.  The number for rows will not be the same as the number for columns when the number of rows (sending nodes) does not equal the number of columns (receiving nodes) -- which is something I'm allowing.</p>
  
  <p>It makes sense to me to do this in <code>numpy</code>, but there may be other packages (like <code>networkx</code>?) that would help.</p>
  
  <p>Here's the function I'm looking to write with the desired inputs and outputs:</p>
  
  <pre><code>n_pre = 4  # number of nodes available to send a connection
  n_post = 4  # number of nodes available to receive a connection
  p = 0.5  # proportion of all possible connections that exist
  
  mat = generate_mat(n_pre, n_post, p)
  
  print mat
  </code></pre>
  
  <p>The output would be, for example:</p>
  
  <pre><code>[[0, 1, 0, 1],
   [1, 0, 1, 0],
   [1, 1, 0, 0],
   [0, 0, 1, 1]]
  </code></pre>
  
  <p>Notice, every column and every row has two ones in it.  Aside from this constraint, the positions of the ones should be random (and vary from call to call of this function).</p>
  
  <p>In graph theory terms, this means every node has an in-degree of 2 and an out-degree of 2 (50% of all possible connections, as specified with <code>p = 0.5</code>).</p>