本文整理匯總了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;
}