本文整理汇总了Java中java.security.SecureRandom.nextDouble方法的典型用法代码示例。如果您正苦于以下问题:Java SecureRandom.nextDouble方法的具体用法?Java SecureRandom.nextDouble怎么用?Java SecureRandom.nextDouble使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.security.SecureRandom
的用法示例。
在下文中一共展示了SecureRandom.nextDouble方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: weightedSamplingWithReplacement
import java.security.SecureRandom; //导入方法依赖的package包/类
public static List<Double> weightedSamplingWithReplacement(List<Double> values,
List<Double> weights, int numberOfSamples) {
int minIndex, maxIndex, sampleIndex;
int size = values.size();
// Calculating the next power of two
int power = (int) Math.ceil(Math.log((double) size) / Math.log(2));
int numPartitions = (int) Math.pow(2, power);
double minValue, maxValue, remainingCapacity, sample, w, elementWeight;
double capacity = 1.00 / numPartitions;
List<Double> partition, samplePartition;
SecureRandom random = new SecureRandom();
List<List<Double>> listPartitions = new ArrayList<>(numPartitions);
for (int i = 0; i < numPartitions; i++) {
partition = new ArrayList<Double>();
minValue = Collections.min(weights);
minIndex = weights.indexOf(minValue);
partition.add((double) minIndex);
if (minValue >= capacity) {
weights.set(minIndex, (minValue - capacity));
partition.add(-1.00);
partition.add(1.00);
} else {
remainingCapacity = capacity - minValue;
maxValue = Collections.max(weights);
maxIndex = weights.indexOf(maxValue);
partition.add((double) maxIndex);
partition.add(minValue / capacity);
weights.set(minIndex, 0.0);
weights.set(maxIndex, (maxValue - remainingCapacity));
}
listPartitions.add(partition);
}
List<Double> samples = new ArrayList<>();
for (int i = 0; i < numberOfSamples; i++) {
sample = random.nextDouble() * numPartitions;
sampleIndex = (int) sample;
w = sample - sampleIndex;
samplePartition = listPartitions.get(sampleIndex);
elementWeight = samplePartition.get(2);
if (w <= elementWeight) {
samples.add(values.get(samplePartition.get(0).intValue()));
} else {
samples.add(values.get(samplePartition.get(1).intValue()));
}
}
return samples;
}