blob: 167468149463c2f5f5d30855c5b45c3a866782eb [file] [log] [blame]
/*
* Copyright 2009-2012 by The Regents of the University of California
* 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 from
*
* 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 edu.uci.ics.hyracks.storage.am.common.datagen;
import java.util.Arrays;
public class ProbabilityHelper {
public static double[] getUniformProbDist(int numChoices) {
double[] probDist = new double[numChoices];
for (int i = 0; i < numChoices; i++) {
probDist[i] = 1.0 / (double) numChoices;
}
return probDist;
}
public static double[] getZipfProbDist(int numChoices, int zipfSkew) {
double[] probDist = new double[numChoices];
double divisor = 0;
for (int i = 1; i <= numChoices; i++) {
divisor += 1.0 / (double) Math.pow((double) i, (double) zipfSkew);
}
for (int i = 1; i <= numChoices; i++) {
probDist[i - 1] = (1.0 / (double) Math.pow((double) i, (double) zipfSkew)) / divisor;
}
return probDist;
}
public static int[] getCumulIntRanges(double[] probDist) {
int[] opRanges = new int[probDist.length];
if (opRanges.length > 1) {
opRanges[0] = (int) Math.floor(Integer.MAX_VALUE * probDist[0]);
for (int i = 1; i < opRanges.length - 1; i++) {
opRanges[i] = opRanges[i - 1] + (int) Math.floor(Integer.MAX_VALUE * probDist[i]);
}
opRanges[opRanges.length - 1] = Integer.MAX_VALUE;
} else {
opRanges[0] = Integer.MAX_VALUE;
}
return opRanges;
}
public static int choose(int[] cumulIntRanges, int randomInt) {
int rndVal = Math.abs(randomInt);
int ix = Arrays.binarySearch(cumulIntRanges, rndVal);
if (ix < 0) {
ix = -ix - 1;
}
return ix;
}
}