SoftNet-Consult Java Utility Library

com.softnetConsult.utils.random
Class FastRandom

java.lang.Object
  extended by java.util.Random
      extended by com.softnetConsult.utils.random.FastRandom
All Implemented Interfaces:
java.io.Serializable

public class FastRandom
extends java.util.Random

Provides a fast pseudorandom number generator by trading off memory for speed. Objects of this class generate a large number of random numbers when they are created and reuse them throughout the life-time. For many applications the repetition effect does not significantly affect the quality of the random series given that the size of the buffer sufficiently large. This class is usefull when a time-critical application requires a very large number of random numbers; a slight delay for the buffer creation at the start saves a lot of time during the runtime.
Note that the current version of this class accelerates only the generation of pseudorandom values of types boolean, double and int: pseudorandom values are pre-generated and cached, pseudorandom values of other types are generated on the fly by passing the nextXXX()-calls to the default Java pseudorandom number generator.

This product includes software developed by the SoftNet-Consult Java Utility Library project and its contributors.
(http://java-tools.sourceforge.net)
Copyright (c) 2007-2008 SoftNet-Consult.
Copyright (c) 2007-2008 G. Paperin.
All rights reserved.

File: FastRandom.java
Library API version: "2.02"
Java compliance version: "1.5"

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following terms and conditions are met:

1. Redistributions of source code must retain the above acknowledgement of the SoftNet-Consult Java Utility Library project, the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above acknowledgement of the SoftNet-Consult Java Utility Library project, the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software or any derived software must display the following acknowledgement:
This product includes software developed by the SoftNet-Consult Java Utility Library project and its contributors.

THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Version:
"2.02"
Author:
Greg Paperin (http://www.paperin.org)
See Also:
Random, Serialized Form

Field Summary
private  boolean[] booleanBuffer
          The buffer for random booleans.
private  int booleanCursor
          Cursor for the boolean random numbers buffer.
static int defaultBooleanBufferSize
          Default buffer size for random booleans.
static int defaultDoubleBufferSize
          Default buffer size for random doubles.
static int defaultIntBufferSize
          Default buffer size for random ints.
private  double[] doubleBuffer
          The buffer for random doubles.
private  int doubleCursor
          Cursor for the double random numbers buffer.
private static boolean generateDebugPrintout
          Used internally for debug purposes.
private  int[] intBuffer
          The buffer for random ints.
private  int intCursor
          Cursor for the int random numbers buffer.
private  boolean ready
          Flag to show that this object has readily initialised.
 
Constructor Summary
FastRandom()
          Creates a new FastRandom with default buffer sizes.
FastRandom(int bufferSize)
          Creates a new FastRandom with buffers of the specified size.
FastRandom(int booleanBufferSize, int doubleBufferSize, int intBufferSize)
          Creates a new FastRandom with buffers of the specified size.
FastRandom(long seed)
          Creates a new FastRandom with default buffer sizes and a specified random seed.
FastRandom(long seed, int bufferSize)
          Creates a new FastRandom with buffers of the specified size a specified random seed.
FastRandom(long seed, int booleanBufferSize, int doubleBufferSize, int intBufferSize)
          Creates a new FastRandom with buffers of the specified size a specified random seed.
 
Method Summary
private  void createBuffers(int booleanBufferSize, int doubleBufferSize, int intBufferSize)
          Allocates the random buffer arrays.
 boolean nextBoolean()
          Returns the next pseudorandom boolean.
 double nextDouble()
          Returns the next pseudorandom double.
 int nextInt()
          Returns the next pseudorandom int.
private  void resetBuffers()
          Used internally to fill the pseudorandom numbers buffers with values.
 void setSeed(long seed)
          Sets the seed of this random number generator and then re-computes all buffers.
 
Methods inherited from class java.util.Random
next, nextBytes, nextFloat, nextGaussian, nextInt, nextLong
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

generateDebugPrintout

private static final boolean generateDebugPrintout
Used internally for debug purposes.

See Also:
Constant Field Values

defaultBooleanBufferSize

public static final int defaultBooleanBufferSize
Default buffer size for random booleans.

See Also:
Constant Field Values

defaultDoubleBufferSize

public static final int defaultDoubleBufferSize
Default buffer size for random doubles.

See Also:
Constant Field Values

defaultIntBufferSize

public static final int defaultIntBufferSize
Default buffer size for random ints.

See Also:
Constant Field Values

booleanBuffer

private boolean[] booleanBuffer
The buffer for random booleans.


doubleBuffer

private double[] doubleBuffer
The buffer for random doubles.


intBuffer

private int[] intBuffer
The buffer for random ints.


booleanCursor

private int booleanCursor
Cursor for the boolean random numbers buffer.


doubleCursor

private int doubleCursor
Cursor for the double random numbers buffer.


intCursor

private int intCursor
Cursor for the int random numbers buffer.


ready

private boolean ready
Flag to show that this object has readily initialised.

Constructor Detail

FastRandom

public FastRandom()
Creates a new FastRandom with default buffer sizes.


FastRandom

public FastRandom(long seed)
Creates a new FastRandom with default buffer sizes and a specified random seed.

Parameters:
seed - Inital random seed.

FastRandom

public FastRandom(int bufferSize)
Creates a new FastRandom with buffers of the specified size.

Parameters:
bufferSize - The size for the random numbers buffers.

FastRandom

public FastRandom(long seed,
                  int bufferSize)
Creates a new FastRandom with buffers of the specified size a specified random seed.

Parameters:
seed - Inital random seed.
bufferSize - The size for the random numbers buffers.

FastRandom

public FastRandom(int booleanBufferSize,
                  int doubleBufferSize,
                  int intBufferSize)
Creates a new FastRandom with buffers of the specified size.

Parameters:
booleanBufferSize - The size for the random boolean numbers buffer.
doubleBufferSize - The size for the random double numbers buffer.
intBufferSize - The size for the random int numbers buffer.

FastRandom

public FastRandom(long seed,
                  int booleanBufferSize,
                  int doubleBufferSize,
                  int intBufferSize)
Creates a new FastRandom with buffers of the specified size a specified random seed.

Parameters:
seed - Inital random seed.
booleanBufferSize - The size for the random boolean numbers buffer.
doubleBufferSize - The size for the random double numbers buffer.
intBufferSize - The size for the random int numbers buffer.
Method Detail

createBuffers

private void createBuffers(int booleanBufferSize,
                           int doubleBufferSize,
                           int intBufferSize)
Allocates the random buffer arrays.

Parameters:
booleanBufferSize - The size for the random boolean numbers buffer.
doubleBufferSize - The size for the random double numbers buffer.
intBufferSize - The size for the random int numbers buffer.

setSeed

public void setSeed(long seed)
Sets the seed of this random number generator and then re-computes all buffers.

Overrides:
setSeed in class java.util.Random
Parameters:
seed - Inital random seed.
See Also:
Random.setSeed(long)

nextBoolean

public boolean nextBoolean()
Returns the next pseudorandom boolean. The buffered random booleans are used as source. Once the buffer has been used up, it is used again from the start.

Overrides:
nextBoolean in class java.util.Random
Returns:
A pseudorandom boolean.
See Also:
Random.nextBoolean()

nextDouble

public double nextDouble()
Returns the next pseudorandom double. The buffered random doubles are used as source. Once the buffer has been used up, it is used again from the start.

Overrides:
nextDouble in class java.util.Random
Returns:
A pseudorandom double.
See Also:
Random.nextDouble()

nextInt

public int nextInt()
Returns the next pseudorandom int. The buffered random ints are used as source. Once the buffer has been used up, it is used again from the start.

Overrides:
nextInt in class java.util.Random
Returns:
A pseudorandom int.
See Also:
Random.nextInt()

resetBuffers

private void resetBuffers()
Used internally to fill the pseudorandom numbers buffers with values.


SoftNet-Consult Java Utility Library is a member of SourceForge.net