import java.util.Random; public class ArrayFiller { long seed; int sparsity; private boolean covering; /** * @param seed * the seed for the random number generator * @param sparsity * the average interval between successive values in the array (hopefully) * @param covering * whether the M array should be made to cover the range of possible values of sums of pairs of elements in the L array */ public ArrayFiller(long seed, int sparsity, boolean covering) { super(); this.seed = seed; this.sparsity = sparsity; this.covering = covering; } public void fillArrays(long[] l, long[] m) { Random rnd = new Random(seed); int lMax = l.length * sparsity; fillArray(l, rnd, lMax); int mMax = covering ? (lMax * 2) : (m.length * sparsity); fillArray(m, rnd, mMax); } private void fillArray(long[] array, Random rnd, int max) { int min = 1; // never 0, for the benefit of hashtables long range = max - min; for (int i = 0; i < array.length; ++i) { array[i] = nextLong(rnd, range) + min; } } protected static long nextLong(Random rnd, long range) { return Math.abs(rnd.nextLong()) % range; // dodgy but close enough } }