如何在Java中的连续方法调用中获取相同的随机数

时间:2017-01-31 03:23:00

标签: java random random-seed

考虑以下代码(使用Java 1.8.0_102和JDistLib 0.4.5):

import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

import jdistlib.Exponential;
import jdistlib.generic.GenericDistribution;
import jdistlib.rng.MersenneTwister;
import jdistlib.rng.RandomEngine;


public class RVs {

    public void getEV(GenericDistribution dist, int sampleSize) {
        ArrayList<Double> rvs = this.populateArray(dist, sampleSize);
        System.out.println(rvs);
    }

    public ArrayList<Double> populateArray(GenericDistribution dist, int sampleSize) {
        RandomEngine randomE = new MersenneTwister(System.currentTimeMillis());
        ArrayList<Double> rvs = new ArrayList<Double>();
        IntStream.range(0, sampleSize).forEach($ -> rvs.add(dist.random(randomE)));
        return rvs;
    }

    public void test () {
        IntStream.range(0, 13).forEach($ -> this.getEV(new Exponential(33.33), 10));
    }

}

......像这样实施:

public class CallRVs {
    public static void main(String[] args) {
        RVs alpha = new RVs();
        alpha.test();
    }
}

不幸的是,这总是重复相同的随机数序列约3-4次。

在结束上面的代码之前,我尝试了这个

  • 未明确设置RandomEngine和
  • 没有将RandomEngine的随机种子设置为当前时间

你会认为,创建种子设置为当前时间的新RandomEngine实例应该会有所帮助,但是没有:

[22.703723551648405, 6.7092516027839855, 190.59060286639905, 1.8735125522919636, 41.022344668942665, 3.877018716199701, 21.49930713290334, 42.05773014708266, 23.00082916381309, 23.24308297023846]
[22.334558402867305, 1.6401554044082725, 84.11326045120587, 18.165389107596983, 83.13647054950233, 32.72909304649951, 31.453740499105955, 93.5956252958078, 4.040938139470317, 19.99121603725849]
[22.334558402867305, 1.6401554044082725, 84.11326045120587, 18.165389107596983, 83.13647054950233, 32.72909304649951, 31.453740499105955, 93.5956252958078, 4.040938139470317, 19.99121603725849]
[22.334558402867305, 1.6401554044082725, 84.11326045120587, 18.165389107596983, 83.13647054950233, 32.72909304649951, 31.453740499105955, 93.5956252958078, 4.040938139470317, 19.99121603725849]
[79.14217486099653, 41.08293664856576, 7.224345566610367, 127.53056431952126, 80.37600520651415, 38.4618180996009, 0.9904925074615941, 12.20987288657261, 36.530623436560845, 32.1600825790288]
[79.14217486099653, 41.08293664856576, 7.224345566610367, 127.53056431952126, 80.37600520651415, 38.4618180996009, 0.9904925074615941, 12.20987288657261, 36.530623436560845, 32.1600825790288]
[79.14217486099653, 41.08293664856576, 7.224345566610367, 127.53056431952126, 80.37600520651415, 38.4618180996009, 0.9904925074615941, 12.20987288657261, 36.530623436560845, 32.1600825790288]
[79.14217486099653, 41.08293664856576, 7.224345566610367, 127.53056431952126, 80.37600520651415, 38.4618180996009, 0.9904925074615941, 12.20987288657261, 36.530623436560845, 32.1600825790288]
[9.273608510364928, 3.489400227955458, 39.90486871467796, 0.62827300597674, 11.400150691547177, 5.284720630096435, 24.384279251275796, 16.08800783422188, 21.318680903820088, 5.974180125899405]
[9.273608510364928, 3.489400227955458, 39.90486871467796, 0.62827300597674, 11.400150691547177, 5.284720630096435, 24.384279251275796, 16.08800783422188, 21.318680903820088, 5.974180125899405]
[9.273608510364928, 3.489400227955458, 39.90486871467796, 0.62827300597674, 11.400150691547177, 5.284720630096435, 24.384279251275796, 16.08800783422188, 21.318680903820088, 5.974180125899405]
[24.382314535207747, 19.911193724734368, 11.447193297362048, 0.9268859488114056, 18.486786238992774, 29.54231307250453, 17.592849710828943, 26.563616563286097, 19.548355104754787, 4.339118659323733]
[24.382314535207747, 19.911193724734368, 11.447193297362048, 0.9268859488114056, 18.486786238992774, 29.54231307250453, 17.592849710828943, 26.563616563286097, 19.548355104754787, 4.339118659323733]

防止此类重复的正确方法是什么?锁(会让一切都变得过慢,对吧?)?用一些计数器或计数器随机增加“腌制”种子(这将是一个非常脏的黑客,其统计后果将不清楚)?

或者我做错了什么? (我也得到“RVs.java:22:警告:GenericDistribution中的[deprecation] random(RandomEngine)已被弃用”,这可能表明这不是正确的方法。)

0 个答案:

没有答案