C# Class PeterO.RandomGenerator

A class that adapts a random byte generator to generate random numbers in a variety of statistical distributions.

The method descriptions in this class assume the underlying random byte generator generates uniformly distributed numbers that are independent of each other.

Thread safety: The methods in this class are safe for concurrent use by multiple threads, as long as the underlying random byte generator is as well.

Show file Open project: peteroupc/CBOR Class Usage Examples

Public Methods

Method Description
Bernoulli ( ) : bool

Returns either true or false at a 50% chance each.

Bernoulli ( double p ) : bool

Returns either true or false, depending on the given probability.

Binomial ( int trials ) : int

Conceptually, generates either 1 or 0 the given number of times, where either number is equally likely, and counts the number of 1's generated.

Binomial ( int trials, double p ) : int

Conceptually, generates either 1 or 0 the given number of times, where a 1 is generated at the given probability, and counts the number of 1's generated.

ChiSquared ( int df ) : double

Generates a random number that is the sum of the squares of "df" normally-distributed random numbers with a mean of 0 and a standard deviation of 1.

Exponential ( ) : double

Not documented yet.

Gamma ( double a ) : double

Not documented yet.

Gamma ( double a, double b ) : double

Not documented yet.

Geometric ( ) : int

Conceptually, generates either 1 or 0 until a 1 is generated, and counts the number of 0's generated. Either number has an equal probability of being generated.

Geometric ( double p ) : int

Conceptually, generates either 1 or 0 until a 1 is generated, and counts the number of 0's generated. A 1 is generated at the given probability.

Hypergeometric ( int trials, int ones, int count ) : int

Conceptually, given a set of tokens, some of which are labeled 1 and the others labeled 0, draws "trials" tokens at random without replacement and then counts the number of 1's drawn.

LogNormal ( double mean, double sd ) : double

Generates a logarithmic normally-distributed number with the given mean and standard deviation.

NegativeBinomial ( int trials ) : int

Conceptually, generates either 1 or 0 the given number of times until the given number of 1's are generated, and counts the number of 0's generated. Either number has an equal probability of being generated.

NegativeBinomial ( int trials, double p ) : int

Conceptually, generates either 1 or 0 until the given number of 1's are generated, and counts the number of 0's generated. A 1 is generated at the given probability.

Normal ( ) : double

Generates a normally-distributed number with mean 0 and standard deviation 1.

Normal ( double mean, double sd ) : double

Generates a normally-distributed number with the given mean and standard deviation.

Poisson ( double mean ) : int

Generates a random integer such that the average of random numbers approaches the given mean number when this method is called repeatedly with the same mean.

RandomGenerator ( ) : System

Initializes a new instance of the RandomGenerator class.

RandomGenerator ( IRandomGen valueIrg ) : System

Initializes a new instance of the RandomGenerator class.

Uniform ( ) : double

Returns a uniformly-distributed 64-bit floating-point number from 0 and up, but less than 1.

Uniform ( double max ) : double

Returns a uniformly-distributed 64-bit floating-point number from 0 and up, but less than the given number.

Uniform ( double min, double max ) : double

Not documented yet.

UniformInt ( int maxExclusive ) : int

Generates a random 32-bit signed integer 0 or greater and less than the given number.

UniformInt ( int minInclusive, int maxExclusive ) : int

Generates a random 32-bit signed integer within a given range.

UniformLong ( long maxExclusive ) : long

Generates a random 32-bit signed integer 0 or greater and less than the given number.

UniformLong ( long minInclusive, long maxExclusive ) : long

Generates a random 64-bit signed integer within a given range.

UniformSingle ( ) : double

Returns a uniformly-distributed 32-bit floating-point number from 0 and up, but less than 1.

Private Methods

Method Description
NonZeroUniform ( ) : double

Method Details

Bernoulli() public method

Returns either true or false at a 50% chance each.
public Bernoulli ( ) : bool
return bool

Bernoulli() public method

Returns either true or false, depending on the given probability.
public Bernoulli ( double p ) : bool
p double A probability from 0 through 1. 0 means always /// false, and 1 means always true.
return bool

Binomial() public method

Conceptually, generates either 1 or 0 the given number of times, where either number is equally likely, and counts the number of 1's generated.
public Binomial ( int trials ) : int
trials int The number of times to generate a random /// number, conceptually.
return int

Binomial() public method

Conceptually, generates either 1 or 0 the given number of times, where a 1 is generated at the given probability, and counts the number of 1's generated.
public Binomial ( int trials, double p ) : int
trials int The number of times to generate a random /// number, conceptually.
p double The probability for each trial to succeed, from 0 /// (never) to 1 (always).
return int

ChiSquared() public method

Generates a random number that is the sum of the squares of "df" normally-distributed random numbers with a mean of 0 and a standard deviation of 1.
public ChiSquared ( int df ) : double
df int Degrees of freedom (the number of independently /// chosen normally-distributed numbers).
return double

Exponential() public method

Not documented yet.
public Exponential ( ) : double
return double

Gamma() public method

Not documented yet.
public Gamma ( double a ) : double
a double Another 64-bit floating-point number.
return double

Gamma() public method

Not documented yet.
public Gamma ( double a, double b ) : double
a double Another 64-bit floating-point number.
b double A 64-bit floating-point number. (3).
return double

Geometric() public method

Conceptually, generates either 1 or 0 until a 1 is generated, and counts the number of 0's generated. Either number has an equal probability of being generated.
public Geometric ( ) : int
return int

Geometric() public method

Conceptually, generates either 1 or 0 until a 1 is generated, and counts the number of 0's generated. A 1 is generated at the given probability.
public Geometric ( double p ) : int
p double A 64-bit floating-point number.
return int

Hypergeometric() public method

Conceptually, given a set of tokens, some of which are labeled 1 and the others labeled 0, draws "trials" tokens at random without replacement and then counts the number of 1's drawn.
public Hypergeometric ( int trials, int ones, int count ) : int
trials int The number of tokens drawn at random without /// replacement.
ones int The number of tokens labeled 1.
count int The number of tokens labeled 1 or 0.
return int

LogNormal() public method

Generates a logarithmic normally-distributed number with the given mean and standard deviation.
public LogNormal ( double mean, double sd ) : double
mean double The desired mean.
sd double Standard deviation.
return double

NegativeBinomial() public method

Conceptually, generates either 1 or 0 the given number of times until the given number of 1's are generated, and counts the number of 0's generated. Either number has an equal probability of being generated.
public NegativeBinomial ( int trials ) : int
trials int The number of 1's to generate before the /// process stops.
return int

NegativeBinomial() public method

Conceptually, generates either 1 or 0 until the given number of 1's are generated, and counts the number of 0's generated. A 1 is generated at the given probability.
public NegativeBinomial ( int trials, double p ) : int
trials int The number of 1's to generate before the /// process stops.
p double The probability for each trial to succeed, from 0 /// (never) to 1 (always).
return int

Normal() public method

Generates a normally-distributed number with mean 0 and standard deviation 1.
public Normal ( ) : double
return double

Normal() public method

Generates a normally-distributed number with the given mean and standard deviation.
public Normal ( double mean, double sd ) : double
mean double The desired mean.
sd double Standard deviation.
return double

Poisson() public method

Generates a random integer such that the average of random numbers approaches the given mean number when this method is called repeatedly with the same mean.
public Poisson ( double mean ) : int
mean double The expected mean of the random numbers.
return int

RandomGenerator() public method

Initializes a new instance of the RandomGenerator class.
public RandomGenerator ( ) : System
return System

RandomGenerator() public method

Initializes a new instance of the RandomGenerator class.
public RandomGenerator ( IRandomGen valueIrg ) : System
valueIrg IRandomGen An IRandomGen object.
return System

Uniform() public method

Returns a uniformly-distributed 64-bit floating-point number from 0 and up, but less than 1.
public Uniform ( ) : double
return double

Uniform() public method

Returns a uniformly-distributed 64-bit floating-point number from 0 and up, but less than the given number.
public Uniform ( double max ) : double
max double Number that the randomly-generated number will be /// less than.
return double

Uniform() public method

Not documented yet.
public Uniform ( double min, double max ) : double
min double Smallest possible number that will be /// generated.
max double Number that the randomly-generated number will be /// less than.
return double

UniformInt() public method

Generates a random 32-bit signed integer 0 or greater and less than the given number.
public UniformInt ( int maxExclusive ) : int
maxExclusive int One plus the largest possible value of /// the random number.
return int

UniformInt() public method

Generates a random 32-bit signed integer within a given range.
public UniformInt ( int minInclusive, int maxExclusive ) : int
minInclusive int Smallest possible value of the random /// number.
maxExclusive int One plus the largest possible value of /// the random number.
return int

UniformLong() public method

Generates a random 32-bit signed integer 0 or greater and less than the given number.
public UniformLong ( long maxExclusive ) : long
maxExclusive long One plus the largest possible value of /// the random number.
return long

UniformLong() public method

Generates a random 64-bit signed integer within a given range.
public UniformLong ( long minInclusive, long maxExclusive ) : long
minInclusive long Smallest possible value of the random /// number.
maxExclusive long One plus the largest possible value of /// the random number.
return long

UniformSingle() public method

Returns a uniformly-distributed 32-bit floating-point number from 0 and up, but less than 1.
public UniformSingle ( ) : double
return double