C# Class RandomOps.Random

Implements a Gaussian RNG using the Uniform() method.
Exibir arquivo Open project: DanWBR/dwsim3 Class Usage Examples

Public Methods

Method Description
Bool ( ) : bool

Draw a random boolean with equal probability of true or false. Thread-safe if Uniform() is thread-safe.

Bool ( double p ) : bool

Draw a random boolean with probability p of true and (1-p) of false. Thread-safe if Uniform() is thread-safe.

Byte ( ) : byte

Draw a random and uniform byte. Thread-safe if Index() is thread-safe.

The default implementation uses Index() which in turn uses Uniform(). This is quite expensive for generating just one byte, but is the most generally viable way of doing it for many PRNGs, because their low-order bits are not very random. Some PRNGs may offer much better ways of generating just a single random byte, in which case this method should of course be overrided.

Bytes ( int length ) : byte[]

Draw an array of random and uniform bytes. Thread-safe if Byte() is thread-safe.

Circle ( ) : double[]

Generate a uniform random point from the unit-radius 2-dimensional circle. Thread-safe if Disk() is thread-safe.

Circle ( double &x, double &y ) : void

Generate a uniform random point from the unit-radius 2-dimensional circle. Thread-safe if Disk() is thread-safe.

Disk ( ) : double[]

Generate a uniform random point from the unit-radius 2-dimensional disk. Thread-safe if Uniform() is thread-safe.

Disk ( double &x, double &y, double &sumSquares ) : void

Generate a uniform random point from the unit-radius 2-dimensional disk. Thread-safe if Uniform() is thread-safe.

Gauss ( ) : double

Draw a Gaussian (or normally) distributed random number, with mean 0 and deviation 1. Not thread-safe.

Gauss ( double mean, double deviation ) : double

Draw a Gaussian (or normally) distributed random number, with designated mean and deviation. Thread-safe if Gauss() is thread-safe.

Index ( double probabilities ) : int

Draw a random integer from the set {0, .., n-1} according to the supplied probability distribution. Probabilities are assumed to sum to one. Time-complexity is O(n), where n is the length of the probabilities-array. Use this function if you only need to draw a few random numbers, otherwise create an IndexDistribution-object. Thread-safe if Uniform() is thread-safe.

Index ( int n ) : int

Draw a random integer from the set {0, .., n-1} with uniform probability. Thread-safe if Uniform() is thread-safe.

The default implementation uses Uniform() to generate an integer. This is because many PRNGs must use division instead of modulo arithmetics due to the lower-order bits having little randomness. Assume Uniform cannot generate an exact value of one, otherwise this must have been taken into account to ensure uniform probability of choosing the different indices.

Index2 ( int n, int &index1, int &index2 ) : void

Draw two distinct integers from the set {0, .., n-1} with equal probability. Thread-safe if Uniform() is thread-safe.

Sphere ( int n, double r ) : double[]

Generate a uniform random point on the n-dimensional hypersphere. Thread-safe if Gauss() is thread-safe.

Sphere ( double &x, double r ) : void

Generate a uniform random point on the n-dimensional hypersphere. Thread-safe if Gauss() is thread-safe, and each thread supplies its own array x.

Sphere3 ( ) : double[]

Generate a uniform random point on the unit-radius 3-dimensional sphere. Thread-safe if Disk() is thread-safe.

Sphere3 ( double &x ) : void

Generate a uniform random point on the unit-radius 3-dimensional sphere. Thread-safe if Disk() is thread-safe.

Sphere4 ( ) : double[]

Generate a uniform random point on the unit-radius 4-dimensional sphere. Thread-safe if Disk() is thread-safe.

Sphere4 ( double &x ) : void

Generate a uniform random point on the unit-radius 4-dimensional sphere. Thread-safe if Disk() is thread-safe.

Uniform ( ) : double

Draw a uniform random number in the exclusive range (0,1).

This must be implemented in every inherited class. It is important that it does not return the end-point values of zero or one, or else some of the methods which use Uniform() to generate their random numbers will not work.

Uniform ( double low, double high ) : double

Draw a uniform random number in the exclusive range (low,high). Thread-safe if Uniform() is thread-safe.

Method Details

Bool() public method

Draw a random boolean with equal probability of true or false. Thread-safe if Uniform() is thread-safe.
public Bool ( ) : bool
return bool

Bool() public method

Draw a random boolean with probability p of true and (1-p) of false. Thread-safe if Uniform() is thread-safe.
public Bool ( double p ) : bool
p double
return bool

Byte() public method

Draw a random and uniform byte. Thread-safe if Index() is thread-safe.
The default implementation uses Index() which in turn uses Uniform(). This is quite expensive for generating just one byte, but is the most generally viable way of doing it for many PRNGs, because their low-order bits are not very random. Some PRNGs may offer much better ways of generating just a single random byte, in which case this method should of course be overrided.
public Byte ( ) : byte
return byte

Bytes() public method

Draw an array of random and uniform bytes. Thread-safe if Byte() is thread-safe.
public Bytes ( int length ) : byte[]
length int The array length requested.
return byte[]

Circle() public method

Generate a uniform random point from the unit-radius 2-dimensional circle. Thread-safe if Disk() is thread-safe.
public Circle ( ) : double[]
return double[]

Circle() public method

Generate a uniform random point from the unit-radius 2-dimensional circle. Thread-safe if Disk() is thread-safe.
public Circle ( double &x, double &y ) : void
x double Random point x
y double Random point y
return void

Disk() public method

Generate a uniform random point from the unit-radius 2-dimensional disk. Thread-safe if Uniform() is thread-safe.
public Disk ( ) : double[]
return double[]

Disk() public method

Generate a uniform random point from the unit-radius 2-dimensional disk. Thread-safe if Uniform() is thread-safe.
public Disk ( double &x, double &y, double &sumSquares ) : void
x double Random point x
y double Random point y
sumSquares double Equals x*x + y*y
return void

Gauss() public method

Draw a Gaussian (or normally) distributed random number, with mean 0 and deviation 1. Not thread-safe.
public Gauss ( ) : double
return double

Gauss() public method

Draw a Gaussian (or normally) distributed random number, with designated mean and deviation. Thread-safe if Gauss() is thread-safe.
public Gauss ( double mean, double deviation ) : double
mean double The mean of the distribution, e.g. 0.
deviation double The deviation of the distribution, e.g. 1.
return double

Index() public method

Draw a random integer from the set {0, .., n-1} according to the supplied probability distribution. Probabilities are assumed to sum to one. Time-complexity is O(n), where n is the length of the probabilities-array. Use this function if you only need to draw a few random numbers, otherwise create an IndexDistribution-object. Thread-safe if Uniform() is thread-safe.
public Index ( double probabilities ) : int
probabilities double Probability distribution.
return int

Index() public method

Draw a random integer from the set {0, .., n-1} with uniform probability. Thread-safe if Uniform() is thread-safe.
The default implementation uses Uniform() to generate an integer. This is because many PRNGs must use division instead of modulo arithmetics due to the lower-order bits having little randomness. Assume Uniform cannot generate an exact value of one, otherwise this must have been taken into account to ensure uniform probability of choosing the different indices.
public Index ( int n ) : int
n int
return int

Index2() public method

Draw two distinct integers from the set {0, .., n-1} with equal probability. Thread-safe if Uniform() is thread-safe.
public Index2 ( int n, int &index1, int &index2 ) : void
n int
index1 int Reference to the first integer drawn.
index2 int Reference to the second integer drawn.
return void

Sphere() public method

Generate a uniform random point on the n-dimensional hypersphere. Thread-safe if Gauss() is thread-safe.
public Sphere ( int n, double r ) : double[]
n int Dimensionality of hypersphere.
r double Radius of hypersphere.
return double[]

Sphere() public method

Generate a uniform random point on the n-dimensional hypersphere. Thread-safe if Gauss() is thread-safe, and each thread supplies its own array x.
public Sphere ( double &x, double r ) : void
x double Array to hold the random point.
r double Radius of hypersphere.
return void

Sphere3() public method

Generate a uniform random point on the unit-radius 3-dimensional sphere. Thread-safe if Disk() is thread-safe.
public Sphere3 ( ) : double[]
return double[]

Sphere3() public method

Generate a uniform random point on the unit-radius 3-dimensional sphere. Thread-safe if Disk() is thread-safe.
public Sphere3 ( double &x ) : void
x double Array to hold the random point.
return void

Sphere4() public method

Generate a uniform random point on the unit-radius 4-dimensional sphere. Thread-safe if Disk() is thread-safe.
public Sphere4 ( ) : double[]
return double[]

Sphere4() public method

Generate a uniform random point on the unit-radius 4-dimensional sphere. Thread-safe if Disk() is thread-safe.
public Sphere4 ( double &x ) : void
x double Array to hold the random point.
return void

Uniform() public abstract method

Draw a uniform random number in the exclusive range (0,1).
This must be implemented in every inherited class. It is important that it does not return the end-point values of zero or one, or else some of the methods which use Uniform() to generate their random numbers will not work.
public abstract Uniform ( ) : double
return double

Uniform() public method

Draw a uniform random number in the exclusive range (low,high). Thread-safe if Uniform() is thread-safe.
public Uniform ( double low, double high ) : double
low double The lowest value in the range.
high double The highest value in the range.
return double