java.util.Random.nextGaussian()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(7.8k)|赞(0)|评价(0)|浏览(265)

本文整理了Java中java.util.Random.nextGaussian()方法的一些代码示例,展示了Random.nextGaussian()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Random.nextGaussian()方法的具体详情如下:
包路径:java.util.Random
类名称:Random
方法名:nextGaussian

Random.nextGaussian介绍

[英]Returns a pseudo-random (approximately) normally distributed double with mean 0.0 and standard deviation 1.0. This method uses the polar method of G. E. P. Box, M. E. Muller, and G. Marsaglia, as described by Donald E. Knuth in The Art of Computer Programming, Volume 2: Seminumerical Algorithms, section 3.4.1, subsection C, algorithm P.
[中]返回平均值为0.0、标准偏差为1.0的伪随机(近似)正态分布双精度。该方法使用G.E.P.Box、M.E.Muller和G.Marsaglia的极坐标法,如Donald E.Knuth在《计算机编程艺术》第2卷:半数值算法,第3.4.1节,C小节,算法P中所述。

代码示例

代码示例来源:origin: h2oai/h2o-3

public static double [] startCoefs(int n, long seed){
  double [] res = MemoryManager.malloc8d(n);
  Random r = new Random(seed);
  for(int i = 0; i < res.length; ++i)
   res[i] = r.nextGaussian();
  return res;
 }
}

代码示例来源:origin: google/guava

private static ImmutableList<Double> generatePseudorandomDataset() {
 Random random = new Random(2211275185798966364L);
 ImmutableList.Builder<Double> largeDatasetBuilder = ImmutableList.builder();
 for (int i = 0; i < PSEUDORANDOM_DATASET_SIZE; i++) {
  largeDatasetBuilder.add(random.nextGaussian());
 }
 return largeDatasetBuilder.build();
}

代码示例来源:origin: stackoverflow.com

public static void main(String[] args) {
  TrendLine t = new PolyTrendLine(2);
  Random rand = new Random();
  double[] x = new double[1000*1000];
  double[] err = new double[x.length];
  double[] y = new double[x.length];
  for (int i=0; i<x.length; i++) { x[i] = 1000*rand.nextDouble(); }
  for (int i=0; i<x.length; i++) { err[i] = 100*rand.nextGaussian(); } 
  for (int i=0; i<x.length; i++) { y[i] = x[i]*x[i]+err[i]; } // quadratic model
  t.setValues(y,x);
  System.out.println(t.predict(12)); // when x=12, y should be... , eg 143.61380202745192
}

代码示例来源:origin: GlowstoneMC/Glowstone

@Override
public void setAngry(boolean angry) {
  if (!angry) {
    anger = 0;
  } else if (isAngry()) {
    anger = (int) (new Random().nextGaussian() * 400) + 400;
  }
}

代码示例来源:origin: RoaringBitmap/RoaringBitmap

private int[] generateArray() {
 Random random = new Random();
 List<Integer> ints = new ArrayList<>(size);
 int last = 0;
 for (int i = 0; i < size; ++i) {
  if (random.nextGaussian() > 1 - randomness) {
   last = last + 1;
  } else {
   last = last + 1 + random.nextInt(99);
  }
  ints.add(last);
 }
 Collections.shuffle(ints);
 int[] data = new int[size];
 int i = 0;
 for (Integer value : ints) {
  data[i++] = value;
 }
 return data;
}

代码示例来源:origin: stanfordnlp/CoreNLP

/**
 * For testing only.
 * @param args Ignored
 */
public static void main(String[] args) {
 Random random = new Random();
 int length = 100;
 double[] A = new double[length];
 double[] B = new double[length];
 double aAvg = 70.0;
 double bAvg = 70.5;
 for (int i = 0; i < length; i++) {
  A[i] = aAvg + random.nextGaussian();
  B[i] = bAvg + random.nextGaussian();
 }
 System.out.println("A has length " + A.length + " and mean " + mean(A));
 System.out.println("B has length " + B.length + " and mean " + mean(B));
 for (int t = 0; t < 10; t++) {
  System.out.println("p-value: " + sigLevelByApproxRand(A, B));
 }
}

代码示例来源:origin: RoaringBitmap/RoaringBitmap

private int[] generateArray(double runThreshold) {
 Random random = new Random();
 int[] data = new int[size];
 int last = 0;
 int i = 0;
 while (i < size) {
  if (random.nextGaussian() > runThreshold) {
   int runLength = random.nextInt(Math.min(size - i, 1 << 16));
   for (int j = 0; j < runLength; ++j) {
    data[i + j] = last + 1;
    last = data[i + j];
   }
   i += runLength;
  } else {
   data[i] = last + 1 + random.nextInt(999);
   last = data[i];
   ++i;
  }
 }
 Arrays.sort(data);
 return data;
}

代码示例来源:origin: apache/flink

final Random random = new Random(firstSeed);
final String tmpDir = System.getProperty("java.io.tmpdir");
    point[0] = random.nextGaussian();
    point[1] = 2 * point[0] + 0.01 * random.nextGaussian();
    writePoint(point, buffer, pointsOut);

代码示例来源:origin: micrometer-metrics/micrometer

@Setup(Level.Iteration)
  public void setup() {
    final Random r = new Random(1234567891L);
    dataIterator = Iterators.cycle(
        Stream.generate(() -> Math.round(Math.exp(2.0 + r.nextGaussian()))).limit(1048576)
            .collect(Collectors.toList()));
  }
}

代码示例来源:origin: h2oai/h2o-2

public static void gauss(double[][] goals, double[][] array) {
 Random rand = new Random(SEED);
 for( int r = 0; r < array.length; r++ ) {
  final int goal = rand.nextInt(goals.length);
  for( int c = 0; c < array[r].length; c++ )
   array[r][c] = goals[goal][c] + rand.nextGaussian() * SIGMA;
 }
}

代码示例来源:origin: prestodb/presto

public static Dataset getDataset()
  {
    int datapoints = 100;
    List<Double> labels = new ArrayList<>();
    List<FeatureVector> features = new ArrayList<>();
    Random rand = new Random(0);
    for (int i = 0; i < datapoints; i++) {
      double label = rand.nextDouble() < 0.5 ? 0 : 1;
      labels.add(label);
      features.add(new FeatureVector(0, label + rand.nextGaussian()));
    }

    return new Dataset(labels, features, ImmutableMap.of(0, "first", 1, "second"));
  }
}

代码示例来源:origin: apache/flink

final Random random = new Random(firstSeed);
      point[d] = (random.nextGaussian() * absoluteStdDev) + centroid[d];

代码示例来源:origin: iSoron/uhabits

public void onRandomize()
{
  Random random = new Random();
  habit.getRepetitions().removeAll();
  double strength = 50;
  for (int i = 0; i < 365 * 5; i++)
  {
    if (i % 7 == 0) strength = max(0, min(100, strength + 10 * random.nextGaussian()));
    if (random.nextInt(100) > strength) continue;
    int value = 1;
    if (habit.isNumerical())
      value = (int) (1000 + 250 * random.nextGaussian() * strength / 100) * 1000;
    habit.getRepetitions().add(new Repetition(DateUtils.getToday().minus(i), value));
  }
  habit.invalidateNewerThan(Timestamp.ZERO);
}

代码示例来源:origin: apache/kylin

private PercentileAggregator createPercentileAggreator(int sumNums, Integer sqrtNum, Integer compression) {
  compression = compression == null ? DEFAULT_COMPRESSION : compression;
  PercentileAggregator aggregator = new PercentileAggregator(compression);
  Random random = new Random();
  for (int i = 0; i < sumNums; i++) {
    double d = 0;
    if (sqrtNum == null)
      d = random.nextInt(1000000000);
    else
      d = Math.sqrt(sqrtNum.intValue()) * random.nextGaussian();
    PercentileCounter c = new PercentileCounter(compression, 0.5);
    c.add(d);
    aggregator.aggregate(c);
  }
  return aggregator;
}

代码示例来源:origin: apache/incubator-druid

final int numRand = 10000;
ApproximateHistogram h = new ApproximateHistogram(combinedHistSize);
Random rand = new Random(0);
Float[] randNums = new Float[numRand];
for (int i = 0; i < numRand; i++) {
 randNums[i] = (float) rand.nextGaussian();
 ApproximateHistogram tmp = new ApproximateHistogram(histSize);
 for (int i = 0; i < 20; ++i) {
  tmp.offer((float) (rand.nextGaussian() + (double) k));

代码示例来源:origin: stackoverflow.com

private static final int FAST = 100;
private static final int SLOW = FAST * 5;
private static final Random random = new Random();
private Timer timer;
  return (float) (random.nextGaussian() * MINMAX / 3);

代码示例来源:origin: prestodb/presto

private static Page getPage()
  {
    Type mapType = typeManager.getParameterizedType("map", ImmutableList.of(TypeSignatureParameter.of(parseTypeSignature(StandardTypes.BIGINT)), TypeSignatureParameter.of(parseTypeSignature(StandardTypes.DOUBLE))));
    int datapoints = 100;
    RowPageBuilder builder = RowPageBuilder.rowPageBuilder(BIGINT, mapType, VarcharType.VARCHAR);
    Random rand = new Random(0);
    for (int i = 0; i < datapoints; i++) {
      long label = rand.nextDouble() < 0.5 ? 0 : 1;
      builder.row(label, mapBlockOf(BIGINT, DOUBLE, 0L, label + rand.nextGaussian()), "C=1");
    }

    return builder.build();
  }
}

代码示例来源:origin: stackoverflow.com

XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
XYSeries series = new XYSeries("Random");
Random rand = new Random();
for (int i = 0; i < values.length; i++) {
  for (int j = 0; j < values[i].length; j++) {
    double x = rand.nextGaussian();
    double y = rand.nextGaussian();
    series.add(x, y);

代码示例来源:origin: stackoverflow.com

Random r = new Random();
double mySample = r.nextGaussian()*desiredStandardDeviation+desiredMean;

代码示例来源:origin: apache/mahout

public static Matrix randomHierarchicalMatrix(int numRows, int numCols, boolean symmetric) {
 Matrix matrix = new DenseMatrix(numRows, numCols);
 // TODO rejigger tests so that it doesn't expect this particular seed
 Random r = new Random(1234L);
 for (int row = 0; row < numRows; row++) {
  Vector v = new DenseVector(numCols);
  for (int col = 0; col < numCols; col++) {
   double val = r.nextGaussian();
   v.set(col, val);
  }
  v.assign(Functions.MULT, 1/((row + 1) * v.norm(2)));
  matrix.assignRow(row, v);
 }
 if (symmetric) {
  return matrix.times(matrix.transpose());
 }
 return matrix;
}

相关文章