net.finmath.randomnumbers.MersenneTwister Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of finmath-lib Show documentation
Show all versions of finmath-lib Show documentation
finmath lib is a Mathematical Finance Library in Java.
It provides algorithms and methodologies related to mathematical finance.
The newest version!
/*
* (c) Copyright Christian P. Fries, Germany. Contact: [email protected].
*
* Created on 17.06.2016
*/
package net.finmath.randomnumbers;
import java.io.Serializable;
/**
* Mersenne Twister random number generator. This class is just a wrapper of
* org.apache.commons.math3.random.MersenneTwister
.
*
* @author Christian Fries
*
* @version 1.0
*/
public class MersenneTwister implements RandomNumberGenerator1D, Serializable {
private static final long serialVersionUID = -1827470318370174186L;
private final Long seed;
private final org.apache.commons.math3.random.MersenneTwister mersenneTwister;
public MersenneTwister(final long seed) {
this.seed = seed;
mersenneTwister = new org.apache.commons.math3.random.MersenneTwister(seed);
}
public MersenneTwister() {
this.seed = System.currentTimeMillis() + System.identityHashCode(this); // This is the same way as MT would create the seed.
mersenneTwister = new org.apache.commons.math3.random.MersenneTwister(seed);
}
/**
* Returns the next random number in the sequence.
*
* @return The next random number in the sequence.
*/
@Override
public double nextDouble() {
synchronized (mersenneTwister) {
return mersenneTwister.nextDouble();
}
}
@Override
public double nextDoubleFast() {
return mersenneTwister.nextDouble();
}
@Override
public String toString() {
return "MersenneTwister [seed=" + seed + "]";
}
}