/*
 * Copyright 2015 Charles University in Prague
 * Copyright 2015 Vojtech Horky
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cz.cuni.mff.d3s.spl.utils;

import java.util.Arrays;

import org.apache.commons.math3.random.EmpiricalDistribution;

/** Helper method for creating mathematical distributions.
 */
public class DistributionUtils  {
	private static int countDistinctValues(double[] samples, double epsilon) {
		if (samples.length <= 1) {
			return 1;
		}
		
		double[] data = Arrays.copyOf(samples, samples.length);
		Arrays.sort(data);
		
		int count = 1;
		double previous = data[0];
		for (double value : data) {
			if (Math.abs(previous - value) > epsilon) {
				previous = value;
				count++;
			}
		}
		return count;
	}
	
	/** Create empirical distribution from given samples.
	 * 
	 * @param samples Samples (does not need to be distinct) to use.
	 * @return Empirical distribution built from the samples.
	 */
	public static EmpiricalDistribution makeEmpirical(double[] samples) {
		int values = countDistinctValues(samples, 0.001);
		
		EmpiricalDistribution distr = new EmpiricalDistribution(values/10 + 1);
		distr.load(samples);	
		
		return distr;
	}
}

@@ -685,580 +685,277 @@
 ort 
-java.util.Arrays;%0A%0Aimport org.apache.commons.math3.random.EmpiricalDistribution;%0A%0A/** Helper method for creating mathematical distributions.%0A */%0Apublic class DistributionUtils  %7B%0A%09private static int countDistinctValues(double%5B%5D samples, double epsilon) %7B%0A%09%09if (samples.length %3C= 1) %7B%0A%09%09%09return 1;%0A%09%09%7D%0A%09%09%0A%09%09double%5B%5D data = Arrays.copyOf(samples, samples.length);%0A%09%09Arrays.sort(data);%0A%09%09%0A%09%09int count = 1;%0A%09%09double previous = data%5B0%5D;%0A%09%09for (double value : data) %7B%0A%09%09%09if (Math.abs(previous - value) %3E epsilon) %7B%0A%09%09%09%09previous = value;%0A%09%09%09%09count++;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return count;%0A%09%7D%0A
+org.apache.commons.math3.random.EmpiricalDistribution;%0Aimport org.apache.commons.math3.random.JDKRandomGenerator;%0Aimport org.apache.commons.math3.random.RandomGenerator;%0A%0A/** Helper method for creating mathematical distributions.%0A */%0Apublic class DistributionUtils  %7B%0A%09
 %09%0A%09/
@@ -1219,55 +1219,105 @@
 %7B%0A%09%09
-int values = countDistinctValues(samples, 0.001
+/* Be deterministic for now. */%0A%09%09RandomGenerator gen = new JDKRandomGenerator();%0A%09%09gen.setSeed(0
 );%0A%09
@@ -1342,21 +1342,22 @@
 ibution 
-distr
+result
  = new E
@@ -1381,31 +1381,47 @@
 ion(
-values/10 + 1);%0A%09%09distr
+samples.length / 10 + 1, gen);%0A%09%09result
 .loa
@@ -1431,17 +1431,16 @@
 amples);
-%09
 %0A%09%09%0A%09%09re
@@ -1444,20 +1444,21 @@
 %09return 
-distr
+result
 ;%0A%09%7D%0A%7D%0A

Make empirical distributions deterministic