mirror of
https://github.com/apache/commons-math.git
synced 2025-03-04 07:29:06 +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;
|
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
|
* Sets the seed of the underyling random number generator using a
|
||||||
* <code>long</code> seed. Sequences of values generated starting with the
|
* <code>long</code> seed. Sequences of values generated starting with the
|
||||||
|
@ -26,6 +26,25 @@ import java.util.Random;
|
|||||||
* @version $Revision$ $Date$
|
* @version $Revision$ $Date$
|
||||||
*/
|
*/
|
||||||
public class JDKRandomGenerator extends Random implements RandomGenerator {
|
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 {
|
public class RandomAdaptor extends Random implements RandomGenerator {
|
||||||
|
|
||||||
/** Serializable version identifier */
|
/** Serializable version identifier. */
|
||||||
private static final long serialVersionUID = 2570805822599485047L;
|
private static final long serialVersionUID = 2306581345647615033L;
|
||||||
|
|
||||||
/** Wrapped randomGenerator instance */
|
/** Wrapped randomGenerator instance */
|
||||||
private RandomGenerator randomGenerator = null;
|
private RandomGenerator randomGenerator = null;
|
||||||
@ -173,17 +173,26 @@ public class RandomAdaptor extends Random implements RandomGenerator {
|
|||||||
return randomGenerator.nextLong();
|
return randomGenerator.nextLong();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** {@inheritDoc} */
|
||||||
* Sets the seed of the underyling random number generator using a
|
public void setSeed(int seed) {
|
||||||
* <code>long</code> seed. Sequences of values generated starting with the
|
if (randomGenerator != null) { // required to avoid NPE in constructor
|
||||||
* same seeds should be identical.
|
randomGenerator.setSeed(seed);
|
||||||
*
|
}
|
||||||
* @param seed the seed value
|
}
|
||||||
*/
|
|
||||||
|
/** {@inheritDoc} */
|
||||||
|
public void setSeed(int[] seed) {
|
||||||
|
if (randomGenerator != null) { // required to avoid NPE in constructor
|
||||||
|
randomGenerator.setSeed(seed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc} */
|
||||||
@Override
|
@Override
|
||||||
public void setSeed(long seed) {
|
public void setSeed(long seed) {
|
||||||
if (randomGenerator != null) { // required to avoid NPE in constructor
|
if (randomGenerator != null) { // required to avoid NPE in constructor
|
||||||
randomGenerator.setSeed(seed);
|
randomGenerator.setSeed(seed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -26,11 +26,32 @@ package org.apache.commons.math.random;
|
|||||||
*/
|
*/
|
||||||
public interface RandomGenerator {
|
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
|
* Sets the seed of the underyling random number generator using a
|
||||||
* <code>long</code> seed. Sequences of values generated starting with the
|
* <code>long</code> seed.
|
||||||
* same seeds should be identical.
|
* <p>Sequences of values generated starting with the same seeds
|
||||||
*
|
* should be identical.
|
||||||
|
* </p>
|
||||||
* @param seed the seed value
|
* @param seed the seed value
|
||||||
*/
|
*/
|
||||||
void setSeed(long seed);
|
void setSeed(long seed);
|
||||||
|
@ -98,7 +98,14 @@ public class RandomAdaptorTest extends RandomDataTest {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setSeed(int seed) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSeed(int[] seed) {
|
||||||
|
}
|
||||||
|
|
||||||
public void setSeed(long seed) {
|
public void setSeed(long seed) {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user