[Numpy-discussion] numpy.random.shuffle

Christopher Barker Chris.Barker at noaa.gov
Wed Nov 22 10:53:19 CST 2006

Robert Kern wrote:
> This is really a thinko on my part.

What, exactly, is a thinko?

> 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.

wouldn't something like:

temp = x[i].copy()
x[i], x[j] = x[j], temp


In any case, it should raise an error if ndim > 1, rather than giving a 
wrong result.


Christopher Barker, Ph.D.

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov

More information about the Numpy-discussion mailing list