Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To generate the array, you can use <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.choice.html" rel="nofollow"><code>numpy.random.choice</code></a>.</p> <pre><code>np.random.choice([0, 1, 2, 3], size=(10,10), p=[.4, .1, .2, .3]) </code></pre> <p>Then you can convert to coordinates. Note that <code>numpy.random.choice</code> generates a random sample using probabilities <code>p</code>, and thus you are not guaranteed to get the <em>exact</em> proportions in <code>p</code>.</p> <p><strong>Extra</strong></p> <p>If you want to have all the <code>1</code>s on a particular side of the array, you can generate two random arrays and then <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.hstack.html#numpy.hstack" rel="nofollow"><code>hstack</code></a> them. The trick is to slightly modify the probabilities of each number on each side.</p> <pre><code>In [1]: import numpy as np In [2]: rem = .1/3 # amount to de- / increase the probability for non-1s In [3]: A = np.random.choice([0, 1, 2, 3], size=(5, 10), p=[.4-rem, .2, .2-rem, .3-rem]) In [4]: B = np.random.choice([0, 2, 3], size=(5, 10), p=[.4+rem, .2+rem, .3+rem]) In [5]: M = np.hstack( (A, B) ) In [6]: M Out[1]: array([[1, 1, 3, 0, 3, 0, 0, 1, 1, 0, 2, 2, 0, 2, 0, 2, 3, 3, 2, 0], [0, 3, 3, 3, 3, 0, 1, 3, 1, 3, 0, 2, 3, 0, 0, 0, 3, 3, 2, 3], [1, 0, 0, 0, 1, 0, 3, 1, 2, 2, 0, 3, 0, 3, 3, 0, 0, 3, 0, 0], [3, 2, 3, 0, 3, 0, 1, 2, 3, 2, 0, 0, 0, 0, 3, 2, 0, 0, 0, 3], [3, 3, 0, 3, 3, 3, 1, 3, 0, 3, 0, 2, 0, 2, 0, 0, 0, 3, 3, 3]]) </code></pre> <p>Here, because I'm putting <em>all</em> the <code>1</code>s on the left, I double the probability of <code>1</code> and decrease the probability of each number equally. The same logic applies when creating the other side.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload