/** @author Scott Marshall @author University of Glasgow, MSc IT Project 2001 @author Building an OnLine Course in Computing Fundamentals Multiple Choice Questions */ package ukacgla_HTMLMulti; import ukacgla_HTMLMulti.*; import java.util.*; /** This class constructs an object whose sole purpose is to randomly re arrange the elements of a vector, without altering the elements themselves. */ public class TestRandomiser { private Random rand; //random number generator private int numQuestions; //size of vector to be returned. public TestRandomiser(int n) { rand = new Random(); numQuestions = n; } //------------------ RANDOMISE METHODS ------------------------------------------ /** Generates a random number between 0(inclusive) and n(exclusive) If n is zero, will throw new IllegalArgumentException("n must be positive"); */ private int genRandom(int n) { Random rand = new Random(); int r = rand.nextInt(n); return r; } /** Copies the elements of a vector into a new Vector, then returns the vector. The returned Vector has the same elements, but in a different order */ protected Vector randomise(Vector original){ Vector copy = new Vector(); while (copy.size() < numQuestions ) { int source = genRandom(original.size()); copy.add(original.get(source)); original.removeElementAt(source); original.trimToSize(); } return copy; }//ends randomise }