cardgame
Class RandomIntGenerator

java.lang.Object
  |
  +--cardgame.RandomIntGenerator

public final class RandomIntGenerator
extends java.lang.Object

The RandomIntGenerator class provides a way to generate random integers within a specific range. It also takes care of reseeding the random number generator after the sequence has been in use for a long time. The default minimum and maximum values have been optimized for use with card games.

Originally appeared in Java Pro, August 2000, page 68.

Version:
1.0, 31 July 2001
Author:
James A. Webb, www.uberkode.com

Field Summary
static int DEFAULT_MAXIMUM_RANGE
          The public variable DEFAULT_MAXIMUM_RANGE defines the largest integer that will be generated when the no-argument constructor is used.
static int DEFAULT_MINIMUM_RANGE
          The public variable DEFAULT_MINIMUM_RANGE defines the smallest integer that will be generated when the no-argument constructor is used.
private  int maximumRange
          The private variable maximumRange defines the largest integer that will be generated and is set by the setRange method.
private  int minimumRange
          The private variable minimumRange defines the smallest integer that will be generated and is set by the setRange method.
private  long numberOfCalls
          The private variable numberOfCalls keeps track of the number of integers that have been generated.
private  java.util.Random random
          The private variable random is a Java Random class object.
private  int range
          The private variable range defines the number of integers that will be generated and is calculated in the setRange method.
 
Constructor Summary
RandomIntGenerator()
          The no-argument constructor creates random numbers within the default limits by supplying the defaults to the two-argument constructor.
RandomIntGenerator(int minRange, int maxRange)
          This constructor creates random numbers within user-specified limits.
 
Method Summary
 int nextRandomInt()
          The public method nextRandomInt is used to generate and return the next random integer.
private  void setRange(int minRange, int maxRange)
          The private method setRange is used by the constructors to set the minimum and maximum range.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

DEFAULT_MINIMUM_RANGE

public static final int DEFAULT_MINIMUM_RANGE
The public variable DEFAULT_MINIMUM_RANGE defines the smallest integer that will be generated when the no-argument constructor is used.
See Also:
RandomIntGenerator(), DEFAULT_MAXIMUM_RANGE

DEFAULT_MAXIMUM_RANGE

public static final int DEFAULT_MAXIMUM_RANGE
The public variable DEFAULT_MAXIMUM_RANGE defines the largest integer that will be generated when the no-argument constructor is used.
See Also:
RandomIntGenerator(), DEFAULT_MINIMUM_RANGE

minimumRange

private int minimumRange
The private variable minimumRange defines the smallest integer that will be generated and is set by the setRange method.
See Also:
maximumRange, setRange(int minRange, int maxRange)

maximumRange

private int maximumRange
The private variable maximumRange defines the largest integer that will be generated and is set by the setRange method.
See Also:
minimumRange, setRange(int minRange, int maxRange)

range

private int range
The private variable range defines the number of integers that will be generated and is calculated in the setRange method.
See Also:
setRange(int minRange, int maxRange)

numberOfCalls

private long numberOfCalls
The private variable numberOfCalls keeps track of the number of integers that have been generated. It is initially set to zero when a new RandomIntGenerator object is constructed. It is tested and reset within the nextRandomInt method.
See Also:
RandomIntGenerator(), RandomIntGenerator(int minRange, int maxRange), nextRandomInt()

random

private java.util.Random random
The private variable random is a Java Random class object. It is constructed using the current system time as a seed. It is reset when needed within the nextRandomInt method.
See Also:
nextRandomInt()
Constructor Detail

RandomIntGenerator

public RandomIntGenerator()
The no-argument constructor creates random numbers within the default limits by supplying the defaults to the two-argument constructor.
See Also:
DEFAULT_MINIMUM_RANGE, DEFAULT_MAXIMUM_RANGE, RandomIntGenerator(int minRange, int maxRange)

RandomIntGenerator

public RandomIntGenerator(int minRange,
                          int maxRange)
This constructor creates random numbers within user-specified limits.
Parameters:
minRange - the smallest number to be generated as an int.
maxRange - the largest number to be generated as an int.
See Also:
minimumRange, maximumRange, setRange(int minRange, int maxRange), random, numberOfCalls
Method Detail

setRange

private void setRange(int minRange,
                      int maxRange)
The private method setRange is used by the constructors to set the minimum and maximum range. It also calculates and sets the overall range (number of integers that will be generated).
Parameters:
minRange - the smallest number to be generated as an int.
maxRange - the largest number to be generated as an int.
See Also:
minimumRange, maximumRange, range

nextRandomInt

public int nextRandomInt()
The public method nextRandomInt is used to generate and return the next random integer.
Returns:
the next random number as an int.
See Also:
random, numberOfCalls