/** @author Scott Marshall @author University of Glasgow, MSc IT Project 2001 @author Building an OnLine Course in Computing Fundamentals */ package ukacgla_KMap; import java.util.*; /** Provides Random numbers generator to fill the k map. The random numbers generated are stored in an array. */ abstract public class KMap_Random { //instance variables private Random rng; private int[] randomNumbers; private int noRequired; /** Creates a new Random number generator, creates an array of specified size and generates required number of numbers (either 0 or 1) to fill the array. */ public KMap_Random(int n){ rng = new Random(); noRequired = n; randomNumbers = new int[noRequired]; genArray(); } /** Generate required number of numbers (either 0 or 1) and store these in array. */ public void genArray() { for(int i = 0 ; i < noRequired ; i++){ randomNumbers[i] = rng.nextInt(2); } } /** Returns the random number stored at the specified index.*/ public int getRandom(int n) { return randomNumbers[n]; } /** Generates a random number between 0(inclusive) and n(exclusive) If n is zero, will throw new IllegalArgumentException("n must be positive"); */ public int genSingle(int n) { int r = rng.nextInt(n); return r; } /** Will copy the elements of a vector into a new Vector, then return the new vector. The new Vector has the same number of elements as the original, but in a different order */ public Vector randomise(Vector original) { Vector copy = new Vector(); int reqSize = original.size(); while (copy.size() < reqSize ) { int source = genSingle(original.size()); copy.add(original.get(source)); original.removeElementAt(source); original.trimToSize(); } return copy; }//ends randomise }