jnt.scimark2.Random Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scimark Show documentation
Show all versions of scimark Show documentation
SciMark 2.0 is a Java benchmark for scientific and numerical
computing. It measures several computational kernels and reports a composite
score in approximate Mflops (Millions of floating point operations per
second).
The newest version!
package jnt.scimark2;
/* Random.java based on Java Numerical Toolkit (JNT) Random.UniformSequence
class. We do not use Java's own java.util.Random so that we can compare
results with equivalent C and Fortran coces.
*/
public class Random {
/* ------------------------------------------------------------------------------
CLASS VARIABLES
------------------------------------------------------------------------------ */
int seed = 0;
private int m[];
private int i = 4;
private int j = 16;
private final int mdig = 32;
private final int one = 1;
private final int m1 = (one << mdig-2) + ((one << mdig-2)-one);
private final int m2 = one << mdig/2;
/* For mdig = 32 : m1 = 2147483647, m2 = 65536
For mdig = 64 : m1 = 9223372036854775807, m2 = 4294967296
*/
private double dm1 = 1.0 / (double) m1;
private boolean haveRange = false;
private double left = 0.0;
private double right = 1.0;
private double width = 1.0;
/* ------------------------------------------------------------------------------
CONSTRUCTORS
------------------------------------------------------------------------------ */
/**
Initializes a sequence of uniformly distributed quasi random numbers with a
seed based on the system clock.
*/
public Random () {
initialize( (int) System.currentTimeMillis());
}
/**
Initializes a sequence of uniformly distributed quasi random numbers on a
given half-open interval [left,right) with a seed based on the system
clock.
@param left (double)
The left endpoint of the half-open interval [left,right).
@param right (double)
The right endpoint of the half-open interval [left,right).
*/
public Random ( double left, double right) {
initialize( (int) System.currentTimeMillis() );
this.left = left;
this.right = right;
width = right - left;
haveRange = true;
}
/**
Initializes a sequence of uniformly distributed quasi random numbers with a
given seed.
@param seed (int)
The seed of the random number generator. Two sequences with the same
seed will be identical.
*/
public Random (int seed) {
initialize( seed);
}
/**
Initializes a sequence of uniformly distributed quasi random numbers
with a given seed on a given half-open interval [left,right).
@param seed (int)
The seed of the random number generator. Two sequences with the same
seed will be identical.
@param left (double)
The left endpoint of the half-open interval [left,right).
@param right (double)
The right endpoint of the half-open interval [left,right).
*/
public Random (int seed, double left, double right) {
initialize( seed);
this.left = left;
this.right = right;
width = right - left;
haveRange = true;
}
/* ------------------------------------------------------------------------------
PUBLIC METHODS
------------------------------------------------------------------------------ */
/**
Returns the next random number in the sequence.
*/
public final synchronized double nextDouble () {
int k;
double nextValue;
k = m[i] - m[j];
if (k < 0) k += m1;
m[j] = k;
if (i == 0)
i = 16;
else i--;
if (j == 0)
j = 16 ;
else j--;
if (haveRange)
return left + dm1 * (double) k * width;
else
return dm1 * (double) k;
}
/**
Returns the next N random numbers in the sequence, as
a vector.
*/
public final synchronized void nextDoubles (double x[])
{
int N = x.length;
int remainder = N & 3; // N mod 4
if (haveRange)
{
for (int count=0; count