/* *
* Title:
* Description:
* Copyright: Copyright (c)
* Company:
* @author
* @version 1.0
*/
package umontreal.iro.lecuyer.rng;
/**
* This container class allows the user to force any {@link RandomStream} to
* return antithetic variates. That is, {@link #nextDouble nextDouble} returns
* 1 - u instead of u and the corresponding change is made in
* {@link #nextInt nextInt}.
* Any instance of this class behaves exactly like a {@link RandomStream},
* except that it depends on another random number generator stream, called the
* base stream, to generate its numbers.
* Any call to one of the next...
* methods of this class will modify the state of the base stream.
*
*/
public class AntitheticStream implements RandomStream {
// The base stream.
private RandomStream st;
/**
* Constructs a new antithetic stream, using the random numbers
* from the base stream stream.
*
*/
public AntitheticStream (RandomStream stream) {
this.st = stream;
}
public void resetStartStream() {
st.resetStartStream();
}
public void resetStartSubstream() {
st.resetStartSubstream();
}
public void resetNextSubstream() {
st.resetNextSubstream();
}
/**
* Returns a string starting with "Antithetic of "
* and finishing with the result of the call to the toString
* method of the generator.
*
*/
public String toString() {
return "Antithetic of " + st.toString();
}
/**
* Returns 1.0 - s.nextDouble() where s is the
* base stream.
*
*/
public double nextDouble() {
return 1.0 - st.nextDouble();
}
/**
* Returns j - i - s.nextInt(i, j) where s is the
* base stream.
*
*/
public int nextInt (int i, int j) {
// pas (j - st.nextInt(0,j-i)), au cas ou le resultat varie.
return j - i - st.nextInt(i, j);
}
/**
* Calls nextArrayOfDouble (u, start, n) for the base stream,
* then replaces each u[i] by 1.0 - u[i].
*
* @param u the array in which the numbers will be stored
*
* @param start the first index of u to be used
*
* @param n the number of random numbers to put in u
*
*
*/
public void nextArrayOfDouble (double[] u, int start, int n) {
st.nextArrayOfDouble (u, start, n);
for (int ii = start; ii < start + n; ii++)
u[ii] = 1.0 - u[ii];
}
/**
* Calls nextArrayOfInt (i, j, u, start, n) for the base stream,
* then replaces each u[i] by j - i - u[i].
*
* @param i the smallest possible integer to put in u
*
* @param j the largest possible integer to put in u
*
* @param u the array in which the numbers will be stored
*
* @param start the first index of u to be used
*
* @param n the number of random numbers to put in u
*
*
*/
public void nextArrayOfInt (int i, int j, int[] u, int start, int n) {
st.nextArrayOfInt (i, j, u, start, n);
for (int ii = start; ii < start + n; ii++)
u[ii] = j - i - u[ii];
}
}
Ads help maintain this website.