[Numpy-discussion] Re: selecting random array element

Robert Kern robert.kern at gmail.com
Mon Feb 20 14:42:06 EST 2006


Alan G Isaac wrote:
> At http://www.american.edu/econ/pytrix/pytrix.py find
> def permute(x):
> 	'''Return a permutation of a sequence or array.
> 
> 	:note: Also consider numpy.random.shuffle
> 	       (to permute *inplace* 1-d arrays)
> 	'''
>         x = numpy.asarray(x)
> 	xshape = x.shape
> 	pidx = numpy.random.random(x.size).argsort()
> 	return x.flat[pidx].reshape(xshape)

You may want to consider numpy.random.permutation()

In [22]: numpy.random.permutation?
Type:           builtin_function_or_method
Base Class:     <type 'builtin_function_or_method'>
String Form:    <built-in method permutation of mtrand.RandomState object at
0x3f0f0>
Namespace:      Interactive
Docstring:
    Given an integer, return a shuffled sequence of integers >= 0 and
    < x; given a sequence, return a shuffled array copy.

    permutation(x)

-- 
Robert Kern
robert.kern at gmail.com

"In the fields of hell where the grass grows high
 Are the graves of dreams allowed to die."
  -- Richard Harter





More information about the NumPy-Discussion mailing list