<p dir="auto"><em>Original ticket <a href="http://projects.scipy.org/numpy/ticket/922" rel="nofollow">http://projects.scipy.org/numpy/ticket/922</a> on 2008-10-02 by <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/josef-pkt/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/josef-pkt">@josef-pkt</a>, assigned to unknown.</em></p>
<p dir="auto">In my fuzz testing of scipy stats, I get sometimes a test failure. I think there is something wrong, random numbers are outside of support of the distribution, with numpy.random.hypergeometric for some cases:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; np.version.version
'1.2.0rc2'"><pre class="notranslate"><code class="notranslate">&gt;&gt;&gt; np.version.version
'1.2.0rc2'
</code></pre></div>
<p dir="auto">signature:<br>
hypergeometric(ngood, nbad, nsample, size=None)</p>
<p dir="auto">when sample size (number of draws, nsample) is small then random numbers look ok:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; np.random.hypergeometric(3,18,8,size=10)
array([2, 1, 0, 2, 1, 1, 2, 1, 0, 3])
&gt;&gt;&gt; np.random.hypergeometric(3,18,9,size=10)
array([2, 2, 0, 0, 1, 0, 1, 1, 3, 1])
&gt;&gt;&gt; np.random.hypergeometric(3,18,10,size=10)
array([0, 2, 2, 0, 2, 2, 2, 0, 0, 2])"><pre class="notranslate"><code class="notranslate">&gt;&gt;&gt; np.random.hypergeometric(3,18,8,size=10)
array([2, 1, 0, 2, 1, 1, 2, 1, 0, 3])
&gt;&gt;&gt; np.random.hypergeometric(3,18,9,size=10)
array([2, 2, 0, 0, 1, 0, 1, 1, 3, 1])
&gt;&gt;&gt; np.random.hypergeometric(3,18,10,size=10)
array([0, 2, 2, 0, 2, 2, 2, 0, 0, 2])
</code></pre></div>
<p dir="auto">for sample size larger than 11 the random number are larger than possible (there are only 3 good balls in urn):</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; np.random.hypergeometric(3,18,11,size=10)
array([18, 16, 16, 17, 18, 17, 18, 17, 17, 16])
&gt;&gt;&gt; np.random.hypergeometric(3,18,12,size=10)
array([17, 16, 16, 18, 17, 17, 18, 17, 16, 17])
&gt;&gt;&gt; np.random.hypergeometric(3,18,13,size=10)
array([18, 17, 17, 16, 17, 17, 16, 16, 18, 16])
&gt;&gt;&gt; np.random.hypergeometric(3,18,14,size=10)
array([16, 17, 17, 17, 17, 18, 17, 16, 18, 17])
&gt;&gt;&gt; np.random.hypergeometric(3,18,15,size=10)
array([18, 18, 17, 16, 17, 16, 17, 17, 18, 18])"><pre class="notranslate"><code class="notranslate">&gt;&gt;&gt; np.random.hypergeometric(3,18,11,size=10)
array([18, 16, 16, 17, 18, 17, 18, 17, 17, 16])
&gt;&gt;&gt; np.random.hypergeometric(3,18,12,size=10)
array([17, 16, 16, 18, 17, 17, 18, 17, 16, 17])
&gt;&gt;&gt; np.random.hypergeometric(3,18,13,size=10)
array([18, 17, 17, 16, 17, 17, 16, 16, 18, 16])
&gt;&gt;&gt; np.random.hypergeometric(3,18,14,size=10)
array([16, 17, 17, 17, 17, 18, 17, 16, 18, 17])
&gt;&gt;&gt; np.random.hypergeometric(3,18,15,size=10)
array([18, 18, 17, 16, 17, 16, 17, 17, 18, 18])
</code></pre></div>
<p dir="auto">reversing the number of good and bad balls -&gt; ok for small sample size:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; np.random.hypergeometric(18,3,5,size=10)    #ok
array([5, 4, 5, 5, 4, 4, 4, 4, 5, 5])
&gt;&gt;&gt; np.random.hypergeometric(18,3,10,size=10)    #ok
array([9, 8, 9, 7, 8, 8, 7, 8, 8, 9])"><pre class="notranslate"><code class="notranslate">&gt;&gt;&gt; np.random.hypergeometric(18,3,5,size=10)    #ok
array([5, 4, 5, 5, 4, 4, 4, 4, 5, 5])
&gt;&gt;&gt; np.random.hypergeometric(18,3,10,size=10)    #ok
array([9, 8, 9, 7, 8, 8, 7, 8, 8, 9])
</code></pre></div>
<p dir="auto">negative numbers for sample size &gt;= 11:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; np.random.hypergeometric(18,3,11,size=10)
array([-5, -5, -5, -7, -4, -5, -5, -4, -6, -6])
&gt;&gt;&gt; np.random.hypergeometric(18,3,13,size=10)
array([-3, -5, -5, -4, -4, -4, -5, -5, -3, -4])
&gt;&gt;&gt; np.random.hypergeometric(18,3,14,size=10)
array([-4, -3, -4, -3, -2, -4, -3, -4, -2, -4])
&gt;&gt;&gt; np.random.hypergeometric(18,3,15,size=10)
array([-2, -2, -1, -1, -3, -2, -2, -2, -1, -2])
&gt;&gt;&gt; np.random.hypergeometric(18,3,16,size=10)
array([-1, -1,  0, -1,  0, -2, -1, -2, -1, -2])"><pre class="notranslate"><code class="notranslate">&gt;&gt;&gt; np.random.hypergeometric(18,3,11,size=10)
array([-5, -5, -5, -7, -4, -5, -5, -4, -6, -6])
&gt;&gt;&gt; np.random.hypergeometric(18,3,13,size=10)
array([-3, -5, -5, -4, -4, -4, -5, -5, -3, -4])
&gt;&gt;&gt; np.random.hypergeometric(18,3,14,size=10)
array([-4, -3, -4, -3, -2, -4, -3, -4, -2, -4])
&gt;&gt;&gt; np.random.hypergeometric(18,3,15,size=10)
array([-2, -2, -1, -1, -3, -2, -2, -2, -1, -2])
&gt;&gt;&gt; np.random.hypergeometric(18,3,16,size=10)
array([-1, -1,  0, -1,  0, -2, -1, -2, -1, -2])
</code></pre></div>
<p dir="auto">Creating an array out of a list that (accidentally) has a circular reference sometimes causes the Python terminal to hang, and can not be terminated even by ctrl-C.</p>
<p dir="auto">Ideally, this should be detected, and an exception should be raised.</p>
<h3 dir="auto">Reproducing code example:</h3>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import numpy as np
a = []
a.append(a)
np.array(a) # outputs `array([[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[list([[...]])]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]], dtype=object)`
a.append(a)
np.array(a)  # hangs"><pre class="notranslate"><code class="notranslate">import numpy as np
a = []
a.append(a)
np.array(a) # outputs `array([[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[list([[...]])]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]], dtype=object)`
a.append(a)
np.array(a)  # hangs
</code></pre></div>
<h3 dir="auto">Error message:</h3>
<p dir="auto">No error - python simply hangs</p>
<h3 dir="auto">Numpy/Python version information:</h3>
<p dir="auto">1.16.4 3.6.8 (default, Apr 25 2019, 21:02:35)<br>
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]</p>