Using Numpy's random.choice to randomly remove item from list
<p>According to the <a href="http://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.random.choice.html" rel="nofollow">http://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.random.choice.html</a>, using the <code>replace = False</code> with Numpy's <code>random.choice</code> method should make the sample without replacement. However, this does not seem to work for me:</p>

<pre><code>In [33]: import numpy as np

In [34]: arr = range(5)

In [35]: number = np.random.choice(arr, replace = False)

In [36]: arr
Out[36]: [0, 1, 2, 3, 4]
</code></pre>

<p>The array <code>arr</code> is still <code>range(5)</code> after sampling, and not missing a (random) number as I would expect. How could I sample a number from <code>range(5)</code> without replacement? </p>

<p>I ended up defining a function using the <code>random</code> library:</p>

<pre><code>import random
def sample_without_replacement(arr):
    random.shuffle(arr)
    return arr.pop()
</code></pre>

<p>Its use is shown below:</p>

<pre><code>In [51]: arr = range(5)

In [52]: number = sample_without_replacement(arr)

In [53]: number
Out[53]: 4

In [54]: arr
Out[54]: [2, 0, 1, 3]
</code></pre>

<p>Note that the method also shuffles the array in place, but for my purposes that doesn't matter.</p>

python numpy random