[Numpy-discussion] numpy.random.shuffle
Robert Kern
robert.kern at gmail.com
Wed Nov 22 10:29:05 CST 2006
Alan G Isaac wrote:
>>>> rr=N.array(zip(range(20),range(20)))
>>>> pid = N.random.permutation(rr.shape[0])
>>>> rr=rr[pid]
>>>> rr
> array([[ 9, 9],
> [ 2, 2],
> [ 3, 3],
> [15, 15],
> [ 4, 4],
> [11, 11],
> [18, 18],
> [17, 17],
> [ 5, 5],
> [12, 12],
> [13, 13],
> [ 1, 1],
> [ 8, 8],
> [14, 14],
> [16, 16],
> [19, 19],
> [ 7, 7],
> [ 6, 6],
> [10, 10],
> [ 0, 0]])
This is really a thinko on my part. I copied the algorithm from Python's random
module. At the core of it is a set of swaps:
x[i], x[j] = x[j], x[i]
With the kind of sequences that the stdlib random module is expecting, that
makes perfect sense. However, with N-dim arrays (N > 1), x[i] is a *view* into
the array. By the time that x[j] = x[i] gets executed, x[i] = x[j] has already
executed and the underlying memory that x[i] points to has been modified.
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
More information about the Numpy-discussion
mailing list