public static int getRandomIndex(double[] weights, double randomDraw) {

		//check randomDraw
		if (0.0 > randomDraw || randomDraw > 1.0) {
			throw new IllegalArgumentException("randomDraw is out of range [0,0] :: " + randomDraw);
		}

		//check weights
		double sum = 0.0;
		for (int i = 0; i < weights.length; i++) {
			if (weights[i] < 0) {
				throw new IllegalArgumentException(
						"Every entry in the weights array must be non-"
						+ "negative, entry " + i + " is :: " + weights[i]);
			}
			sum += weights[i];
		}

		//normalize weights
		double[] normalizedCopy = Arrays.copyOf(weights, weights.length);
		for (int i = 0; i < normalizedCopy.length; i++) {
			normalizedCopy[i] /= sum;
		}

		//calculate answer
		double total = 0.0;
		int i = 0;
		while (total < randomDraw) {
			total += normalizedCopy[i];
			i++;

			//roundoff error might prevent this loop from exiting if randomDraw is VERY close to 1
			if (i > weights.length + 5) {
				throw new IllegalStateException(
						"It appears round-off error has prevented proper execution");
			}
		}

		return i - 1;
	}