Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


random choice should support multidimensional arrays #2724

alan-isaac opened this Issue · 1 comment

2 participants


random.choice enhancement request:

random.choice should support multidimensional arrays by taking an axis number. With this enhancement, axis=None would choose from a flattened array, while an integer argument would chose from the subarrays along that axis.

Apologies if this is a duplicate. My previous submission seems to have vanished.


This should be simple if you are thinking about doing it. All that seems necessary is to add a special check for axis=None and ravel in that case and replace make axis=1. Then set pop_size = a.shape[axis] and pass axis into take. (Plus possibly an error if a is just an int and axis is given).

Actually thinking about it, it could also be reasonable to have size=None as default and have it return a scalar/the given axes removed in that case. That would be a real change in functionality unfortunately, but it would make sense for similarity to import random; random.choice mostly.

One could think about also allowing an out argument which is just passed into take mostly. While none of the other random functions have that it could be a nice performance option in some cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.