mirror of
https://github.com/apache/commons-math.git
synced 2025-03-03 23:19:05 +00:00
added new seeding methods for random generators
this will allow implementing some modern and efficient generators that require a lots of bits for seeding in the form of complete integer arrays git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@796543 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
5fd4a00932
commit
05d4f8539c
@ -61,6 +61,23 @@ public abstract class AbstractRandomGenerator implements RandomGenerator {
|
||||
cachedNormalDeviate = Double.NaN;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public void setSeed(int seed) {
|
||||
setSeed((long) seed);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public void setSeed(int[] seed) {
|
||||
// the following number is the largest prime that fits in 32 bits (it is 2^32 - 5)
|
||||
final long prime = 4294967291l;
|
||||
|
||||
long combined = 0l;
|
||||
for (int s : seed) {
|
||||
combined = combined * prime + s;
|
||||
}
|
||||
setSeed(combined);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the seed of the underyling random number generator using a
|
||||
* <code>long</code> seed. Sequences of values generated starting with the
|
||||
|
@ -26,6 +26,25 @@ import java.util.Random;
|
||||
* @version $Revision$ $Date$
|
||||
*/
|
||||
public class JDKRandomGenerator extends Random implements RandomGenerator {
|
||||
/** Serializable version identifier */
|
||||
private static final long serialVersionUID = -3561898582944940550L;
|
||||
|
||||
/** Serializable version identifier. */
|
||||
private static final long serialVersionUID = -7745277476784028798L;
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public void setSeed(int seed) {
|
||||
setSeed((long) seed);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public void setSeed(int[] seed) {
|
||||
// the following number is the largest prime that fits in 32 bits (it is 2^32 - 5)
|
||||
final long prime = 4294967291l;
|
||||
|
||||
long combined = 0l;
|
||||
for (int s : seed) {
|
||||
combined = combined * prime + s;
|
||||
}
|
||||
setSeed(combined);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -27,8 +27,8 @@ import java.util.Random;
|
||||
*/
|
||||
public class RandomAdaptor extends Random implements RandomGenerator {
|
||||
|
||||
/** Serializable version identifier */
|
||||
private static final long serialVersionUID = 2570805822599485047L;
|
||||
/** Serializable version identifier. */
|
||||
private static final long serialVersionUID = 2306581345647615033L;
|
||||
|
||||
/** Wrapped randomGenerator instance */
|
||||
private RandomGenerator randomGenerator = null;
|
||||
@ -173,17 +173,26 @@ public class RandomAdaptor extends Random implements RandomGenerator {
|
||||
return randomGenerator.nextLong();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the seed of the underyling random number generator using a
|
||||
* <code>long</code> seed. Sequences of values generated starting with the
|
||||
* same seeds should be identical.
|
||||
*
|
||||
* @param seed the seed value
|
||||
*/
|
||||
/** {@inheritDoc} */
|
||||
public void setSeed(int seed) {
|
||||
if (randomGenerator != null) { // required to avoid NPE in constructor
|
||||
randomGenerator.setSeed(seed);
|
||||
}
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public void setSeed(int[] seed) {
|
||||
if (randomGenerator != null) { // required to avoid NPE in constructor
|
||||
randomGenerator.setSeed(seed);
|
||||
}
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public void setSeed(long seed) {
|
||||
if (randomGenerator != null) { // required to avoid NPE in constructor
|
||||
randomGenerator.setSeed(seed);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -26,11 +26,32 @@ package org.apache.commons.math.random;
|
||||
*/
|
||||
public interface RandomGenerator {
|
||||
|
||||
/**
|
||||
* Sets the seed of the underyling random number generator using an
|
||||
* <code>int</code> seed.
|
||||
* <p>Sequences of values generated starting with the same seeds
|
||||
* should be identical.
|
||||
* </p>
|
||||
* @param seed the seed value
|
||||
*/
|
||||
void setSeed(int seed);
|
||||
|
||||
/**
|
||||
* Sets the seed of the underyling random number generator using an
|
||||
* <code>int</code> array seed.
|
||||
* <p>Sequences of values generated starting with the same seeds
|
||||
* should be identical.
|
||||
* </p>
|
||||
* @param seed the seed value
|
||||
*/
|
||||
void setSeed(int[] seed);
|
||||
|
||||
/**
|
||||
* Sets the seed of the underyling random number generator using a
|
||||
* <code>long</code> seed. Sequences of values generated starting with the
|
||||
* same seeds should be identical.
|
||||
*
|
||||
* <code>long</code> seed.
|
||||
* <p>Sequences of values generated starting with the same seeds
|
||||
* should be identical.
|
||||
* </p>
|
||||
* @param seed the seed value
|
||||
*/
|
||||
void setSeed(long seed);
|
||||
|
@ -98,7 +98,14 @@ public class RandomAdaptorTest extends RandomDataTest {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public void setSeed(int seed) {
|
||||
}
|
||||
|
||||
public void setSeed(int[] seed) {
|
||||
}
|
||||
|
||||
public void setSeed(long seed) {
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user