Re: Randomize order of an array?
Posted by
ctrueden on
Feb 27, 2009; 10:26pm
URL: http://imagej.273.s1.nabble.com/Randomize-order-of-an-array-tp3693530p3693534.html
Hi Johannes,
Did you not mean to decrement n later in the loop?
>
Well, I did steal it straight from the Wikipedia page, but I tested and it
works as written.
Intuitively, the n-- in in the right place because ImageJ macro arrays are
0-indexed, n starts at array.length, and the loop ends when n == 1.
-Curtis
On Fri, Feb 27, 2009 at 4:15 PM, Johannes Schindelin <
[hidden email]> wrote:
> Hi,
>
> On Fri, 27 Feb 2009, Curtis Rueden wrote:
>
> > //---------------------------
> > function shuffle(array) {
> > n = array.length; // The number of items left to shuffle (loop
> > invariant).
> > while (n > 1) {
> > k = randomInt(n); // 0 <= k < n.
> > n--; // n is now the last pertinent index;
>
> Did you not mean to decrement n later in the loop?
>
> > temp = array[n]; // swap array[n] with array[k] (does nothing if
> k
> > == n).
> > array[n] = array[k];
> > array[k] = temp;
>
> Like here?
>
> Ciao,
> Dscho
>