/*
 * Zorbage: an algebraic data hierarchy for use in numeric processing.
 *
 * Copyright (C) 2016-2020 Barry DeZonia
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package nom.bdezonia.zorbage.algorithm;

import java.util.Random;

import nom.bdezonia.zorbage.algebra.Algebra;
import nom.bdezonia.zorbage.datasource.IndexedDataSource;

/**
 * 
 * @author Barry DeZonia
 *
 */
public class Shuffle {

	// do not instantiate
	
	private Shuffle() { }
	
	/**
	 * 
	 * @param algebra
	 * @param a
	 */
	public static <T extends Algebra<T,U>, U>
		void compute(T algebra, IndexedDataSource<U> a)
	{
		U tmp1 = algebra.construct();
		U tmp2 = algebra.construct();
		Random rng = new Random(System.currentTimeMillis());
		long aSize = a.size();
		for (long i = 0; i < aSize-1; i++) {
			// Safely generate a random long in the right range
			// I'm avoiding nextLong() because it uses a 48-bit generator so it can't cover the
			// entire space.
			long rh = rng.nextInt();
			long rl = rng.nextInt();
			long index = (rh << 32) & rl;
			long mask = aSize-i;
			long upperBit = Long.highestOneBit(mask);
			while (upperBit > 0) {
				mask = mask | upperBit;
				upperBit >>= 1;
			}
			index &= mask;
			if (index < 0)
				index = Math.abs(index);
			a.get(i, tmp1);
			a.get(i+index, tmp2);
			a.set(i, tmp2);
			a.set(i+index, tmp1);
		}
	}
}

@@ -1461,24 +1461,53 @@
 algorithm;%0A%0A
+import java.math.BigInteger;%0A
 import java.
@@ -2229,308 +2229,151 @@
 ace.
-%0A%09%09%09long rh = rng.nextInt();%0A%09%09%09long rl = rng.nextInt();%0A%09%09%09long index = (rh %3C%3C 32) & rl;%0A%09%09%09long mask = aSize-i;%0A%09%09%09long upperBit = Long.highestOneBit(mask);%0A%09%09%09while (upperBit %3E 0) %7B%0A%09%09%09%09mask = mask %7C upperBit;%0A%09%09%09%09upperBit %3E%3E= 1;%0A%09%09%09%7D%0A%09%09%09index &= mask;%0A%09%09%09if (index %3C 0)%0A%09%09%09%09index = Math.abs(index
+ This might not either but it might be a better approach.%0A%09%09%09BigInteger bi = new BigInteger(63, rng);%0A%09%09%09long index = bi.longValue() %25 (aSize-i
 );%0A%09

Improve the correctness and randomness of the shuffle algorithm