Re: Randomize order of an array?
Posted by dscho on Feb 27, 2009; 10:15pm
URL: http://imagej.273.s1.nabble.com/Randomize-order-of-an-array-tp3693530p3693533.html
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