Question

If a seed number is defined for the random number generation, is it possible that different random number sequences are achieved on different computers? If so, how to achieve the same sequences?

    private static final long seed = 1;

    Random generator = new Random(seed);

    for (int i = 0; i < nchrom; i++) {
        val = (int) Math.round(generater.nextDouble()*(nchrom-1));
        //...
    }
Était-ce utile?

La solution

Yes, with the same seed you should get the same sequence of numbers. The algorithm is specified in the documentation:

An instance of this class is used to generate a stream of pseudorandom numbers. The class uses a 48-bit seed, which is modified using a linear congruential formula. (See Donald Knuth, The Art of Computer Programming, Volume 2, Section 3.2.1.)

If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers. In order to guarantee this property, particular algorithms are specified for the class Random. Java implementations must use all the algorithms shown here for the class Random, for the sake of absolute portability of Java code. However, subclasses of class Random are permitted to use other algorithms, so long as they adhere to the general contracts for all the methods.

My only concern would be that if you're using nextDouble() you could run into some artifacts of floating point unit differences. I suspect you won't, but that would be my concern. I'd recommend that you use nextInt anyway:

val = generator.nextInt(nchrom); // Exclusive upper bound
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top