package rpi.goldsd.container;
import java.util.Enumeration;
/**
* The RandomIntSequence class implements the Sequence
* interface by providing a sequence() method that generates an
* enumeration of Int objects using a linear congruential method.
* This is not a very useful class, but rather an "excursion" as part of this
* project.
*
* @see Sequence
* @version 1.0, 4/16/98
* @author David Goldschmidt
*/
public class RandomIntSequence implements Sequence
{
/**
* Returns an enumeration of pseudo-random integer values using
* the linear congruential method with a default value of 25 as the
* seed value. The hasMoreElements() method of the
* enumeration always returns true since the
* nextElement() method will always return the next Int
* object in the sequence.
* @return an enumeration of pseudo-random Int objects.
* @exception IllegalArgumentException if the startValue argument
* is not of type Int.
*/
public Enumeration sequence() { return sequence( new Int( 25 ) ); }
/**
* Returns an enumeration of pseudo-random integer values using the
* given seed value s.
* @param s the seed of the generated pseudo-random sequence.
* @return an enumeration of pseudo-random integer values using the
* given seed value s.
*/
public Enumeration sequence( int s ) { return sequence( new Int( s ) ); }
/**
* Returns an enumeration of pseudo-random integer values using
* the linear congruential method. The hasMoreElements()
* method of the enumeration always returns true since the
* nextElement() method will always return the next Int
* object in the sequence.
* @param startValue the seed value used to begin the linear
* congruential process.
* @return an enumeration of pseudo-random Int objects.
* @exception IllegalArgumentException if the startValue argument
* is not of type Int.
*/
public Enumeration sequence( final Object startValue )
throws IllegalArgumentException
{
if ( ! ( startValue instanceof Int ) ) throw
new IllegalArgumentException( "Start value must be of type Int." );
return ( new Enumeration() {
private int k = 37;
private int c = 87;
private int m = 100;
private int x = ((Int)startValue).value;
public boolean hasMoreElements() { return true; }
public Object nextElement()
{
Int result = new Int(x);
x = ( k * x + c ) % m;
return result;
}
} );
}
}