Kurt Peek - 6 months ago 39

Python Question

According to the http://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.random.choice.html, using the

`replace = False`

`random.choice`

`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]

The array

`arr`

`range(5)`

`range(5)`

Answer

As mentioned in one of the comments, np.choice selects with or without replacement, a series of numbers from a sequence. But it does not modify the sequence.

**Easy alternative**
arr = range(5)
# numbers below will never contain repeated numbers (replace=False)
numbers = np.random.choice(arr, 3, replace=False)

The behaviour I think you want would be:

```
arr = range(5)
all_but_one = np.random.choice(arr, len(arr) -1, replace=False)
```

so you would select N-1 numbers without replacement (to avoid repetitions), effectively removing a random element from the iterable.

**More efficient alternative**

```
arr = range(5)
random_index = np.random.randint(0, len(arr))
arr.pop(random_index)
```