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:
Luc Maisonobe 2009-07-21 21:32:38 +00:00
parent 5fd4a00932
commit 05d4f8539c
5 changed files with 90 additions and 17 deletions

View File

@ -60,7 +60,24 @@ public abstract class AbstractRandomGenerator implements RandomGenerator {
public void clear() {
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

View File

@ -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);
}
}

View File

@ -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);
}
}
}

View File

@ -27,10 +27,31 @@ package org.apache.commons.math.random;
public interface RandomGenerator {
/**
* 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.
*
* 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.
* <p>Sequences of values generated starting with the same seeds
* should be identical.
* </p>
* @param seed the seed value
*/
void setSeed(long seed);

View File

@ -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) {
}
}
}
}