All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cern.jet.random.tfloat.engine.FloatRandomEngine Maven / Gradle / Ivy

Go to download

Parallel Colt is a multithreaded version of Colt - a library for high performance scientific computing in Java. It contains efficient algorithms for data analysis, linear algebra, multi-dimensional arrays, Fourier transforms, statistics and histogramming.

The newest version!
/*
Copyright (C) 1999 CERN - European Organization for Nuclear Research.
Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose 
is hereby granted without fee, provided that the above copyright notice appear in all copies and 
that both that copyright notice and this permission notice appear in supporting documentation. 
CERN makes no representations about the suitability of this software for any purpose. 
It is provided "as is" without expressed or implied warranty.
 */
package cern.jet.random.tfloat.engine;

import cern.jet.random.tdouble.engine.MersenneTwister64;

/**
 * Abstract base class for uniform pseudo-random number generating engines.
 * 

* Most probability distributions are obtained by using a uniform * pseudo-random number generation engine followed by a transformation to the * desired distribution. Thus, subclasses of this class are at the core of * computational statistics, simulations, Monte Carlo methods, etc. *

* Subclasses produce uniformly distributed int's and long's * in the closed intervals [Integer.MIN_VALUE,Integer.MAX_VALUE] and * [Long.MIN_VALUE,Long.MAX_VALUE], respectively, as well as * float's and float's in the open unit intervals * (0.0f,1.0f) and (0.0,1.0), respectively. *

* Subclasses need to override one single method only: nextInt(). All * other methods generating different data types or ranges are usually layered * upon nextInt(). long's are formed by concatenating two 32 * bit int's. float's are formed by dividing the interval * [0.0f,1.0f] into 232 sub intervals, then randomly * choosing one subinterval. float's are formed by dividing the * interval [0.0,1.0] into 264 sub intervals, then randomly * choosing one subinterval. *

* Note that this implementation is not synchronized. * * @author [email protected] * @version 1.0, 09/24/99 * @see FloatMersenneTwister * @see MersenneTwister64 * @see java.util.Random */ // public abstract class RandomEngine extends // edu.cornell.lassp.houle.RngPack.RandomSeedable implements // cern.colt.function.FloatFunction, cern.colt.function.IntFunction { public abstract class FloatRandomEngine extends cern.colt.PersistentObject implements cern.colt.function.tfloat.FloatFunction, cern.colt.function.tint.IntFunction { /** * */ private static final long serialVersionUID = 1L; /** * Makes this class non instantiable, but still let's others inherit from * it. */ protected FloatRandomEngine() { } /** * Equivalent to raw(). This has the effect that random engines can * now be used as function objects, returning a random number upon function * evaluation. */ public float apply(float dummy) { return raw(); } /** * Equivalent to nextInt(). This has the effect that random engines * can now be used as function objects, returning a random number upon * function evaluation. */ public int apply(int dummy) { return nextInt(); } /** * Constructs and returns a new uniform random number engine seeded with the * current time. Currently this is * {@link cern.jet.random.tfloat.engine.FloatMersenneTwister}. */ public static FloatRandomEngine makeDefault() { return new cern.jet.random.tfloat.engine.FloatMersenneTwister((int) System.currentTimeMillis()); } /** * Returns a 32 bit uniformly distributed random number in the open unit * interval (0.0f,1.0f) (excluding 0.0f and 1.0f). */ public float nextFloat() { // catch loss of precision of float --> float conversion float nextFloat; do { nextFloat = raw(); } while (nextFloat >= 1.0f); // --> in (0.0f,1.0f) return nextFloat; } /** * Returns a 32 bit uniformly distributed random number in the closed * interval [Integer.MIN_VALUE,Integer.MAX_VALUE] (including * Integer.MIN_VALUE and Integer.MAX_VALUE); */ public abstract int nextInt(); /** * Returns a 64 bit uniformly distributed random number in the closed * interval [Long.MIN_VALUE,Long.MAX_VALUE] (including * Long.MIN_VALUE and Long.MAX_VALUE). */ public long nextLong() { // concatenate two 32-bit strings into one 64-bit string return ((nextInt() & 0xFFFFFFFFL) << 32) | ((nextInt() & 0xFFFFFFFFL)); } /** * Returns a 32 bit uniformly distributed random number in the open unit * interval (0.0,1.0) (excluding 0.0 and 1.0). */ public float raw() { int nextInt; do { // accept anything but zero nextInt = nextInt(); // in // [Integer.MIN_VALUE,Integer.MAX_VALUE]-interval } while (nextInt == 0); // transform to (0.0,1.0)-interval // 2.3283064365386963E-10 == 1.0 / Math.pow(2,32) return (float) ((nextInt & 0xFFFFFFFFL) * 2.3283064365386963E-10); /* * nextInt == Integer.MAX_VALUE --> 0.49999999976716936 nextInt == * Integer.MIN_VALUE --> 0.5 nextInt == Integer.MAX_VALUE-1 --> * 0.4999999995343387 nextInt == Integer.MIN_VALUE+1 --> * 0.5000000002328306 nextInt == 1 --> 2.3283064365386963E-10 nextInt == * -1 --> 0.9999999997671694 nextInt == 2 --> 4.6566128730773926E-10 * nextInt == -2 --> 0.9999999995343387 */ } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy