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

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

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

Random.nextDouble介绍

[英]Returns a pseudo-random uniformly distributed doublein the half-open range [0.0, 1.0).
[中]返回半开范围[0.0,1.0]内的伪随机均匀分布双精度。

代码示例

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

Random r = new Random();
double randomValue = rangeMin + (rangeMax - rangeMin) * r.nextDouble();

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

double start = 400;
double end = 402;
double random = new Random().nextDouble();
double result = start + (random * (end - start));
System.out.println(result);

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

/**
 * To provide random values to populate a position.
 *
 */
protected Properties getProps() {
 Properties props = new Properties();
 double qty = rng.nextInt(MAX_QTY) * 100.00;
 double mktValue = rng.nextDouble() * MAX_PRICE;
 props.setProperty("qty", String.valueOf(qty));
 props.setProperty("mktValue", String.valueOf(mktValue));
 return props;
}

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

@Test
public void testIllegalNumber() {
  Random rand = new Random(System.currentTimeMillis());
  ArrayList<Double> doubleList = new ArrayList<>();
  int count = 10;
  for (int i = 0; i < count; i++) {
    doubleList.add(rand.nextDouble());
  }
  doubleList.add(0.0);
  doubleList.add(0.0); //test duplicate
  doubleList.add(-1.0); //test negative number
  doubleList.add(Double.MAX_VALUE);
  doubleList.add(-Double.MAX_VALUE);
  //System.out.println(Double.MIN_VALUE);
  System.out.println("test numbers:" + doubleList);
  ArrayList<String> strNumList = listToStringList(doubleList);
  strNumList.add("fjaeif"); //illegal type
  //System.out.println("test num strs list:"+strNumList);
  try {
    ArrayList<SelfDefineSortableKey> keyList = createKeyList(strNumList, (byte) SelfDefineSortableKey.TypeFlag.DOUBLE_FAMILY_TYPE.ordinal());
    Collections.sort(keyList);
    fail("Need catch exception");
  }catch(Exception e){
    //correct
  }
}

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

private List<Double> createDoubleValuesNoNull()
  {
    List<Double> values = new ArrayList<>();
    for (int i = 0; i < ROWS; ++i) {
      values.add(Double.valueOf(random.nextDouble()));
    }
    return values;
  }
}

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

protected double genDouble(List<Double> addTo) {
 double val = rand.nextDouble() * rand.nextInt(100);
 addTo.add(val);
 return val;
}

代码示例来源: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: brianfrankcooper/YCSB

private void delay() {
 final Random random = ThreadLocalRandom.current();
 double p = random.nextDouble();
 int mod;
 if (p < 0.9) {
  System.out.println("OUCH");
  final long baseDelayNs = MICROSECONDS.toNanos(delays[mod]);
  final int delayRangeNs = (int) (MICROSECONDS.toNanos(delays[mod + 1]) - baseDelayNs);
  final long delayNs = baseDelayNs + random.nextInt(delayRangeNs);
  final long deadline = System.nanoTime() + delayNs;
  do {

代码示例来源:origin: kiegroup/optaplanner

@Test
public void nextInt() {
  Random random = mock(Random.class);
  NearbyRandom nearbyRandom = new ParabolicDistributionNearbyRandom(100);
  when(random.nextDouble()).thenReturn(0.0);
  assertEquals(0, nearbyRandom.nextInt(random, 500));
  when(random.nextDouble()).thenReturn(1.0 - Math.pow(1 - 1.0 / 100.0, 3.0));
  assertEquals(1, nearbyRandom.nextInt(random, 500));
  when(random.nextDouble()).thenReturn(1.0 - Math.pow(1 - 2.0 / 100.0, 3.0));
  assertEquals(2, nearbyRandom.nextInt(random, 500));
}

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

Random r = new Random(10);
int numFeatures = 5;
int featureLength = 30;
 for (int j = 0; j < numFeatures; j++) {
  if (r.nextBoolean()) {
   humanFeatureVectors[i].setSparseComponent(j, r.nextInt(featureLength), r.nextDouble());
  } else {
   double[] dense = new double[featureLength];
   for (int k = 0; k < dense.length; k++) {
    dense[k] = r.nextDouble();
for (int i = 0; i < numFeatures; i++) {
 double[] dense = new double[featureLength];
 for (int j = 0; j < dense.length; j++) dense[j] = r.nextDouble();
 weights.setDenseComponent(i, dense);

代码示例来源:origin: SonarSource/sonarqube

@Test
public void getDouble() {
 double value = new Random().nextDouble();
 verifySupportHeadAndOrTrailingWhitespaces(value, Configuration::getDouble);
}

代码示例来源:origin: skylot/jadx

public A test() {
  Random random = new Random();
  int a2 = random.nextInt();
  int a3 = a2 + 3;
  return new A(this, a2, a3, 4, 5, random.nextDouble()) {
    @Override
    public void m() {
      System.out.println(1);
    }
  };
}

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

thisV[i] = generator.nextDouble();
  thisX[i] = generator.nextDouble();
System.out.println("Condition Number of: " + maxSeen/minSeen);
System.out.println("Is negative: " + isNeg);
System.out.println("Is positive: " + isPos);
System.out.println("Is semi:     " + isSemi);

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

@Test
public void testSimpleTypesObjects() {
  SimpleTypes a = new SimpleTypes();
  SimpleTypes b = new SimpleTypes(rnd.nextInt(), rnd.nextLong(), (byte) rnd.nextInt(),
      StringUtils.getRandomString(rnd, 10, 100), (short) rnd.nextInt(), rnd.nextDouble());
  SimpleTypes c = new SimpleTypes(rnd.nextInt(), rnd.nextLong(), (byte) rnd.nextInt(),
      StringUtils.getRandomString(rnd, 10, 100), (short) rnd.nextInt(), rnd.nextDouble());
  SimpleTypes d = new SimpleTypes(rnd.nextInt(), rnd.nextLong(), (byte) rnd.nextInt(),
      StringUtils.getRandomString(rnd, 10, 100), (short) rnd.nextInt(), rnd.nextDouble());
  SimpleTypes e = new SimpleTypes(rnd.nextInt(), rnd.nextLong(), (byte) rnd.nextInt(),
      StringUtils.getRandomString(rnd, 10, 100), (short) rnd.nextInt(), rnd.nextDouble());
  SimpleTypes f = new SimpleTypes(rnd.nextInt(), rnd.nextLong(), (byte) rnd.nextInt(),
      StringUtils.getRandomString(rnd, 10, 100), (short) rnd.nextInt(), rnd.nextDouble());
  SimpleTypes g = new SimpleTypes(rnd.nextInt(), rnd.nextLong(), (byte) rnd.nextInt(),
      StringUtils.getRandomString(rnd, 10, 100), (short) rnd.nextInt(), rnd.nextDouble());
  runTests(a, b, c, d, e, f, g);
}

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

@Test
public void testRandom() {
  Random rand = new Random();
  int n = 1000;
  double[] nums = new double[n];
  for (int i = 0; i < n; i++) {
    nums[i] = rand.nextDouble() * 1000000;
  }
  Arrays.parallelSort(nums);
  buf.clear();
  dds.serialize(nums, buf);
  buf.flip();
  double[] r = dds.deserialize(buf);
  assertArrayEquals(nums, r);
  System.out.println("doubles size of " + (n * 8) + " bytes serialized to " + buf.limit() + " bytes");
}

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

public void generateScanDots()
 {
  // Make sure stars don't jump around between updates.
    // final Random lRandom = new Random(ACearth.getStartTime());

  final int lStarsMax = (int)(fImageWidth * fImageHeight * fStarFrequency);
  for(int i = 0; i < lStarsMax; i++)
  {
   // "-1" to leave space for big stars.
   int x = (int)(lRandom.nextDouble() * (fImageWidth - 1));
   int y = (int)(lRandom.nextDouble() * fImageHeight);

   fDots.add(new ScanDot(ScanDot.DotTypeStar, x, y));

   // A big star is just two pixels wide.
   if((fBigStars != 0) && (Math.random() * 100 < fBigStars))
   {
    fDots.add(new ScanDot(ScanDot.DotTypeStar, x + 1, y));
   }
  }
 }
}

代码示例来源:origin: hankcs/HanLP

int index, count = 0;
index = random.nextInt(siz);  // initial center
docs.add(documents_.get(index));
++count;
double potential = 0.0;
  double randval = random.nextDouble() * potential;
  docs.add(documents_.get(index));
  ++count;

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

int numGroups = 50000;
int itemCount = 30;
Random random = new Random();
GroupedAccumulator groupedAccumulator = createGroupedAccumulator(aggregationFunction);
    String str = String.valueOf(i % 10);
    String item = IntStream.range(0, itemCount).mapToObj(x -> str).collect(Collectors.joining());
    boolean distinctValue = random.nextDouble() < distinctFraction;
    if (distinctValue) {
      valueList.add(item);
      valueList.add(item);

代码示例来源: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: apache/hive

public static int getRandomCodePoint() {
 int codePoint;
 if (rnd.nextDouble() < 0.50) {
  codePoint = 32 + rnd.nextInt(90);
 } else {
  codePoint = getRandomSupplementaryChar();
 }
 if (!Character.isValidCodePoint(codePoint)) {
  System.out.println(Integer.toHexString(codePoint) + " is not a valid code point");
 }
 return codePoint;
}

相关文章