lundi 30 janvier 2017

How to not get the same random numbers in successive method calls in Java

Consider the following code (using Java 1.8.0_102 and 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));
    }

}

... implemented like this:

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

Unfortunately, this always repeats the same sequences of random numbers about 3-4 times.

Before ending up with the code above, I tried this

  • without setting the RandomEngine explicitly and
  • without setting the random seed for the RandomEngine to the current time

You would think, creating new RandomEngine instances with seed set to the current time should help, but no:

[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]

What is the proper way to prevent such repetitions? Locks (would make everything excessively slow, right?)? "Salting" the seed with some counter or counter with random increment (would be an extremely dirty hack the statistical consequences of which would be unclear)?

Or am I doing something wrong? (I also get "RVs.java:22: warning: [deprecation] random(RandomEngine) in GenericDistribution has been deprecated" which might suggest that this is not the right way to do it.)




Aucun commentaire:

Enregistrer un commentaire