当前位置: 首页>>代码示例>>Java>>正文


Java SecureRandom.nextDouble方法代码示例

本文整理汇总了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;

}
 
开发者ID:zoho-labs,项目名称:Explainer,代码行数:52,代码来源:ExplainerUtils.java


注:本文中的java.security.SecureRandom.nextDouble方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。