Class NumPyRandom
- Namespace
- NumSharp
- Assembly
- NumSharp.dll
A class that serves as numpy.random.RandomState in python. Uses MT19937 (Mersenne Twister) for NumPy-compatible random number generation.
public class NumPyRandom
- Inheritance
-
NumPyRandom
- Inherited Members
- Extension Methods
Remarks
Constructors
NumPyRandom()
protected NumPyRandom()
NumPyRandom(MT19937)
protected NumPyRandom(MT19937 bitGenerator)
Parameters
bitGeneratorMT19937
NumPyRandom(NativeRandomState)
protected NumPyRandom(NativeRandomState nativeRandomState)
Parameters
nativeRandomStateNativeRandomState
NumPyRandom(int)
protected NumPyRandom(int seed)
Parameters
seedint
Fields
randomizer
The MT19937 bit generator (NumPy-compatible).
protected MT19937 randomizer
Field Value
Properties
Seed
public int Seed { get; set; }
Property Value
Methods
NextGaussian()
Returns a random sample from the standard normal distribution (mean=0, std=1). Uses the polar method (Marsaglia) matching NumPy's legacy RandomState exactly.
protected double NextGaussian()
Returns
Remarks
NumPy's legacy RandomState uses the polar method (not Box-Muller) with caching. The polar method generates two uniform values in [-1,1], rejects if outside unit circle, then transforms to standard normal. The second value is cached.
This is critical for matching NumPy's randn() output exactly.
RandomState()
Returns a new instance of NumPyRandom.
public NumPyRandom RandomState()
Returns
RandomState(NativeRandomState)
Returns a new instance of NumPyRandom.
public NumPyRandom RandomState(NativeRandomState state)
Parameters
stateNativeRandomState
Returns
RandomState(int)
Returns a new instance of NumPyRandom.
public NumPyRandom RandomState(int seed)
Parameters
seedint
Returns
bernoulli(double)
Draw a single sample from a Bernoulli distribution.
public NDArray bernoulli(double p)
Parameters
pdoubleProbability of success (1), must be in [0, 1].
Returns
- NDArray
A scalar (0 or 1) from the Bernoulli distribution.
bernoulli(double, Shape)
Draw samples from a Bernoulli distribution.
public NDArray bernoulli(double p, Shape size)
Parameters
Returns
- NDArray
Drawn samples (0 or 1) from the Bernoulli distribution.
Remarks
This function is NumSharp-specific and not available in NumPy.
For NumPy equivalent, use scipy.stats.bernoulli.
The Bernoulli distribution is a discrete distribution having two possible
outcomes: 1 (success) with probability p, and 0 (failure) with probability 1-p.
beta(double, double)
Draw a single sample from a Beta distribution.
public NDArray beta(double a, double b)
Parameters
Returns
beta(double, double, Shape)
Draw samples from a Beta distribution.
public NDArray beta(double a, double b, Shape size)
Parameters
Returns
- NDArray
Drawn samples from the parameterized Beta distribution.
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.beta.html
The Beta distribution is a special case of the Dirichlet distribution,
and is related to the Gamma distribution.
binomial(int, double)
Draw a single sample from a binomial distribution.
public NDArray binomial(int n, double p)
Parameters
Returns
binomial(int, double, Shape)
Draw samples from a binomial distribution.
public NDArray binomial(int n, double p, Shape size)
Parameters
nintParameter of the distribution, >= 0. Number of trials.
pdoubleParameter of the distribution, >= 0 and <= 1. Probability of success.
sizeShapeOutput shape.
Returns
- NDArray
Drawn samples from the parameterized binomial distribution, where each sample is equal to the number of successes over the n trials.
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.binomial.html
Samples are drawn from a binomial distribution with specified parameters,
n trials and p probability of success where n is an integer >= 0 and p is
in the interval [0, 1].
chisquare(double)
Draw a single sample from a chi-square distribution.
public NDArray chisquare(double df)
Parameters
dfdouble
Returns
chisquare(double, Shape)
Draw samples from a chi-square distribution.
public NDArray chisquare(double df, Shape size)
Parameters
Returns
- NDArray
Drawn samples from the parameterized chi-square distribution.
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.chisquare.html
When df independent random variables, each with standard normal distributions
(mean 0, variance 1), are squared and summed, the resulting distribution is
chi-square. This distribution is often used in hypothesis testing.
choice(NDArray, Shape, bool, double[])
Generates a random sample from a given 1-D array.
public NDArray choice(NDArray a, Shape size = default, bool replace = true, double[] p = null)
Parameters
aNDArrayArray to sample from.
sizeShapeOutput shape. Default is None, in which case a single value is returned.
replaceboolWhether the sample is with or without replacement. Default is True.
pdouble[]The probabilities associated with each entry in a. If not given, the sample assumes a uniform distribution over all entries.
Returns
- NDArray
The generated random samples.
Remarks
choice(int, Shape, bool, double[])
Generates a random sample from np.arange(a).
public NDArray choice(int a, Shape size = default, bool replace = true, double[] p = null)
Parameters
aintIf an int, the random sample is generated from np.arange(a).
sizeShapeOutput shape. Default is None, in which case a single value is returned.
replaceboolWhether the sample is with or without replacement. Default is True.
pdouble[]The probabilities associated with each entry. If not given, the sample assumes a uniform distribution.
Returns
- NDArray
The generated random samples.
Remarks
choice(long, Shape, bool, double[])
Generates a random sample from np.arange(a).
public NDArray choice(long a, Shape size = default, bool replace = true, double[] p = null)
Parameters
alongIf a long, the random sample is generated from np.arange(a).
sizeShapeOutput shape. Default is None, in which case a single value is returned.
replaceboolWhether the sample is with or without replacement. Default is True.
pdouble[]The probabilities associated with each entry. If not given, the sample assumes a uniform distribution.
Returns
- NDArray
The generated random samples.
Remarks
dirichlet(NDArray, Shape?)
Draw samples from the Dirichlet distribution.
public NDArray dirichlet(NDArray alpha, Shape? size = null)
Parameters
Returns
- NDArray
Drawn samples from the Dirichlet distribution.
dirichlet(double[], int)
Draw samples from the Dirichlet distribution.
public NDArray dirichlet(double[] alpha, int size)
Parameters
Returns
- NDArray
Drawn samples from the Dirichlet distribution.
dirichlet(double[], int[])
Draw samples from the Dirichlet distribution.
public NDArray dirichlet(double[] alpha, int[] size)
Parameters
Returns
- NDArray
Drawn samples from the Dirichlet distribution.
dirichlet(double[], long[])
Draw samples from the Dirichlet distribution.
public NDArray dirichlet(double[] alpha, long[] size)
Parameters
Returns
- NDArray
Drawn samples from the Dirichlet distribution.
dirichlet(double[], Shape?)
Draw samples from the Dirichlet distribution.
public NDArray dirichlet(double[] alpha, Shape? size = null)
Parameters
alphadouble[]Concentration parameters of the distribution (k > 0 elements, each > 0).
sizeShape?Output shape. The output has shape (*size, k) where k is the length of alpha.
Returns
- NDArray
Drawn samples from the Dirichlet distribution. Each row sums to 1.
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.dirichlet.html
The Dirichlet distribution is a distribution over vectors x that fulfil:
- x_i > 0
- sum(x) = 1
The probability density function is: p(x) = (1/B(alpha)) * prod(x_i^(alpha_i - 1))
Algorithm: For each sample, draw Y_i ~ Gamma(alpha_i, 1), then X = Y / sum(Y).
exponential(double)
Draw a single sample from an exponential distribution.
public NDArray exponential(double scale = 1)
Parameters
scaledouble
Returns
exponential(double, Shape)
Draw samples from an exponential distribution.
public NDArray exponential(double scale, Shape size)
Parameters
scaledoubleThe scale parameter, β = 1/λ. Must be non-negative. Default is 1.0.
sizeShapeOutput shape.
Returns
- NDArray
Drawn samples from the parameterized exponential distribution.
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.exponential.html
The exponential distribution is a continuous analogue of the geometric distribution.
It describes many common situations, such as the size of raindrops measured over
many rainstorms, or the time between page requests to Wikipedia.
f(double, double)
Draw a single sample from an F distribution.
public NDArray f(double dfnum, double dfden)
Parameters
Returns
f(double, double, Shape)
public NDArray f(double dfnum, double dfden, Shape size)
Parameters
Returns
gamma(double, double)
Draw a single sample from a Gamma distribution.
public NDArray gamma(double shape, double scale = 1)
Parameters
Returns
gamma(double, double, Shape)
Draw samples from a Gamma distribution.
public NDArray gamma(double shape, double scale, Shape size)
Parameters
shapedoubleThe shape of the gamma distribution. Must be non-negative.
scaledoubleThe scale of the gamma distribution. Must be non-negative. Default is 1.0.
sizeShapeOutput shape.
Returns
- NDArray
Drawn samples from the parameterized gamma distribution.
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.gamma.html
Samples are drawn from a Gamma distribution with specified parameters,
shape (sometimes designated "k") and scale (sometimes designated "theta"),
where both parameters are > 0.
geometric(double)
Draw a single sample from the geometric distribution.
public NDArray geometric(double p)
Parameters
pdouble
Returns
geometric(double, Shape)
Draw samples from the geometric distribution.
public NDArray geometric(double p, Shape size)
Parameters
Returns
- NDArray
Drawn samples from the parameterized geometric distribution.
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.geometric.html
Bernoulli trials are experiments with one of two outcomes: success or failure
(an example of such an experiment is flipping a coin). The geometric distribution
models the number of trials that must be run in order to achieve success.
It is therefore supported on the positive integers, k = 1, 2, ...
get_state()
Return a NativeRandomState representing the internal state of the generator.
public NativeRandomState get_state()
Returns
- NativeRandomState
The current state, including Gaussian cache.
gumbel(double, double)
Draw a single sample from a Gumbel distribution.
public NDArray gumbel(double loc = 0, double scale = 1)
Parameters
Returns
gumbel(double, double, Shape)
Draw samples from a Gumbel distribution (extreme value type I).
public NDArray gumbel(double loc, double scale, Shape size)
Parameters
locdoubleThe location of the mode of the distribution. Default is 0.
scaledoubleThe scale parameter of the distribution. Must be non-negative. Default is 1.
sizeShapeOutput shape.
Returns
- NDArray
Drawn samples from the parameterized Gumbel distribution.
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.gumbel.html
The Gumbel (or Smallest Extreme Value (SEV) or the Smallest Extreme Value Type I)
distribution is used to model the distribution of the maximum (or minimum) of a
number of samples of various distributions.
The probability density function is:
p(x) = (1/scale) * exp(-(x-loc)/scale) * exp(-exp(-(x-loc)/scale))
For Gumbel(loc, scale):
- mean = loc + scale * γ (where γ ≈ 0.5772 is the Euler-Mascheroni constant)
- std = scale * π / sqrt(6) ≈ 1.283 * scale
hypergeometric(long, long, long)
Draw a single sample from a Hypergeometric distribution.
public NDArray hypergeometric(long ngood, long nbad, long nsample)
Parameters
ngoodlongNumber of ways to make a good selection. Must be non-negative.
nbadlongNumber of ways to make a bad selection. Must be non-negative.
nsamplelongNumber of items sampled. Must be >= 1 and <= ngood + nbad.
Returns
- NDArray
A single sample from the hypergeometric distribution as 0-d array.
hypergeometric(long, long, long, int)
Draw samples from a Hypergeometric distribution.
public NDArray hypergeometric(long ngood, long nbad, long nsample, int size)
Parameters
ngoodlongNumber of ways to make a good selection. Must be non-negative.
nbadlongNumber of ways to make a bad selection. Must be non-negative.
nsamplelongNumber of items sampled. Must be >= 1 and <= ngood + nbad.
sizeintOutput shape as single int.
Returns
- NDArray
Drawn samples from the hypergeometric distribution.
hypergeometric(long, long, long, int[])
Draw samples from a Hypergeometric distribution.
public NDArray hypergeometric(long ngood, long nbad, long nsample, int[] size)
Parameters
ngoodlongNumber of ways to make a good selection. Must be non-negative.
nbadlongNumber of ways to make a bad selection. Must be non-negative.
nsamplelongNumber of items sampled. Must be >= 1 and <= ngood + nbad.
sizeint[]Output shape as int array.
Returns
- NDArray
Drawn samples from the hypergeometric distribution.
hypergeometric(long, long, long, long[])
Draw samples from a Hypergeometric distribution.
public NDArray hypergeometric(long ngood, long nbad, long nsample, long[] size)
Parameters
ngoodlongNumber of ways to make a good selection. Must be non-negative.
nbadlongNumber of ways to make a bad selection. Must be non-negative.
nsamplelongNumber of items sampled. Must be >= 1 and <= ngood + nbad.
sizelong[]Output shape.
Returns
- NDArray
Drawn samples from the hypergeometric distribution.
hypergeometric(long, long, long, Shape?)
Draw samples from a Hypergeometric distribution.
public NDArray hypergeometric(long ngood, long nbad, long nsample, Shape? size = null)
Parameters
ngoodlongNumber of ways to make a good selection. Must be non-negative.
nbadlongNumber of ways to make a bad selection. Must be non-negative.
nsamplelongNumber of items sampled. Must be >= 1 and <= ngood + nbad.
sizeShape?Output shape.
Returns
- NDArray
Drawn samples from the hypergeometric distribution (number of good items in sample).
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.hypergeometric.html
Consider an urn with ngood white marbles and nbad black marbles.
If you draw nsample balls without replacement, the hypergeometric distribution
describes the distribution of white balls in the drawn sample.
Mean = nsample * ngood / (ngood + nbad)
laplace(double, double)
Draw a single sample from the Laplace distribution.
public NDArray laplace(double loc = 0, double scale = 1)
Parameters
Returns
laplace(double, double, Shape)
Draw samples from the Laplace or double exponential distribution with specified location (or mean) and scale (decay).
public NDArray laplace(double loc, double scale, Shape size)
Parameters
locdoubleThe position of the distribution peak. Default is 0.
scaledoubleThe exponential decay. Must be non-negative. Default is 1.
sizeShapeOutput shape.
Returns
- NDArray
Drawn samples from the parameterized Laplace distribution.
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.laplace.html
The Laplace distribution is similar to the Gaussian/normal distribution,
but is sharper at the peak and has fatter tails. It represents the
difference between two independent, identically distributed exponential
random variables.
The probability density function is:
f(x; μ, λ) = (1/2λ) * exp(-|x - μ| / λ)
where μ is the location parameter and λ is the scale parameter.
logistic(double, double)
Draw a single sample from a logistic distribution.
public NDArray logistic(double loc = 0, double scale = 1)
Parameters
Returns
logistic(double, double, Shape)
Draw samples from a logistic distribution.
public NDArray logistic(double loc, double scale, Shape size)
Parameters
locdoubleMean of the distribution. Default is 0.
scaledoubleScale parameter (must be >= 0). Default is 1.
sizeShapeOutput shape.
Returns
- NDArray
Drawn samples from the parameterized logistic distribution.
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.logistic.html
The logistic distribution is used in extreme value problems, finance,
and for growth modeling. It is similar to the normal distribution but
has heavier tails.
The probability density function is:
f(x; μ, s) = exp(-(x-μ)/s) / (s * (1 + exp(-(x-μ)/s))^2)
Mean = loc, Variance = scale^2 * pi^2 / 3
lognormal(double, double)
Draw a single sample from a log-normal distribution.
public NDArray lognormal(double mean = 0, double sigma = 1)
Parameters
Returns
lognormal(double, double, Shape)
Draw samples from a log-normal distribution.
public NDArray lognormal(double mean, double sigma, Shape size)
Parameters
meandoubleMean value of the underlying normal distribution. Default is 0.
sigmadoubleStandard deviation of the underlying normal distribution. Must be non-negative. Default is 1.
sizeShapeOutput shape.
Returns
- NDArray
Drawn samples from the parameterized log-normal distribution.
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.lognormal.html
Draw samples from a log-normal distribution with specified mean, standard deviation,
and array shape. Note that the mean and standard deviation are not the values for
the distribution itself, but of the underlying normal distribution it is derived from.
logseries(double, Shape)
Draw samples from a logarithmic series distribution.
public NDArray logseries(double p, Shape size)
Parameters
pdoubleShape parameter for the distribution. Must be in the range [0, 1).
sizeShapeOutput shape.
Returns
- NDArray
Drawn samples from the parameterized logarithmic series distribution.
logseries(double, int)
Draw samples from a logarithmic series distribution.
public NDArray logseries(double p, int size)
Parameters
pdoubleShape parameter for the distribution. Must be in the range [0, 1).
sizeintOutput shape as single int.
Returns
- NDArray
Drawn samples from the parameterized logarithmic series distribution.
logseries(double, int[])
Draw samples from a logarithmic series distribution.
public NDArray logseries(double p, int[] size)
Parameters
pdoubleShape parameter for the distribution. Must be in the range [0, 1).
sizeint[]Output shape as int array.
Returns
- NDArray
Drawn samples from the parameterized logarithmic series distribution.
logseries(double, long[])
Draw samples from a logarithmic series distribution.
public NDArray logseries(double p, long[] size)
Parameters
pdoubleShape parameter for the distribution. Must be in the range [0, 1).
sizelong[]Output shape.
Returns
- NDArray
Drawn samples from the parameterized logarithmic series distribution.
logseries(double, Shape?)
Draw samples from a logarithmic series distribution.
public NDArray logseries(double p, Shape? size = null)
Parameters
pdoubleShape parameter for the distribution. Must be in the range [0, 1).
sizeShape?Output shape. If null, a single value is returned.
Returns
- NDArray
Drawn samples from the parameterized logarithmic series distribution.
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.logseries.html
The probability density for the Log Series distribution is:
P(k) = -p^k / (k * ln(1-p))
The log series distribution is frequently used to represent species
richness and occurrence, first proposed by Fisher, Corbet, and Williams in 1943.
Returns positive integers (k >= 1).
Exceptions
- ArgumentException
If p is not in range [0, 1) or is NaN.
multinomial(int, double[], int)
Draw samples from a multinomial distribution.
public NDArray multinomial(int n, double[] pvals, int size)
Parameters
nintNumber of experiments (>= 0).
pvalsdouble[]Probabilities of each of the k different outcomes. Must sum to ~1.
sizeintNumber of samples to draw.
Returns
- NDArray
Drawn samples with shape (size, k), where each row sums to n.
multinomial(int, double[], int[])
Draw samples from a multinomial distribution.
public NDArray multinomial(int n, double[] pvals, int[] size)
Parameters
nintNumber of experiments (>= 0).
pvalsdouble[]Probabilities of each of the k different outcomes. Must sum to ~1.
sizeint[]Output shape as int array.
Returns
- NDArray
Drawn samples with shape (*size, k), where each row sums to n.
multinomial(int, double[], Shape?)
Draw samples from a multinomial distribution.
public NDArray multinomial(int n, double[] pvals, Shape? size = null)
Parameters
nintNumber of experiments (>= 0).
pvalsdouble[]Probabilities of each of the k different outcomes. Must sum to ~1.
sizeShape?Output shape. Result will have shape (*size, k).
Returns
- NDArray
Drawn samples with shape (*size, k), where each row sums to n.
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.multinomial.html
The multinomial distribution is a multivariate generalization of the binomial
distribution. Each sample represents n experiments, where each experiment
results in one of k possible outcomes.
multivariate_normal(NDArray, NDArray, Shape?, string, double)
Draw random samples from a multivariate normal distribution.
public NDArray multivariate_normal(NDArray mean, NDArray cov, Shape? size = null, string check_valid = "warn", double tol = 1E-08)
Parameters
Returns
multivariate_normal(double[], double[,], int, string, double)
Draw random samples from a multivariate normal distribution.
public NDArray multivariate_normal(double[] mean, double[,] cov, int size, string check_valid = "warn", double tol = 1E-08)
Parameters
Returns
multivariate_normal(double[], double[,], int[])
Draw random samples from a multivariate normal distribution.
public NDArray multivariate_normal(double[] mean, double[,] cov, int[] size)
Parameters
Returns
multivariate_normal(double[], double[,], long[])
Draw random samples from a multivariate normal distribution.
public NDArray multivariate_normal(double[] mean, double[,] cov, long[] size)
Parameters
Returns
multivariate_normal(double[], double[,], Shape?, string, double)
Draw random samples from a multivariate normal distribution.
public NDArray multivariate_normal(double[] mean, double[,] cov, Shape? size = null, string check_valid = "warn", double tol = 1E-08)
Parameters
meandouble[]Mean of the N-dimensional distribution (1D array of length N).
covdouble[,]Covariance matrix of the distribution (N x N symmetric positive-semidefinite).
sizeShape?Output shape. Given a shape of (m, n, k), mnk samples are generated with output shape (m, n, k, N).
check_validstringBehavior when the covariance matrix is not positive semidefinite: "warn", "raise", or "ignore".
toldoubleTolerance when checking covariance matrix validity.
Returns
- NDArray
Drawn samples of shape (*size, N) where N is the length of mean.
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.multivariate_normal.html
The multivariate normal distribution is a generalization of the 1D normal distribution
to higher dimensions. It is specified by its mean vector and covariance matrix.
Algorithm: Uses Jacobi eigendecomposition of the covariance matrix with sign normalization
to match NumPy's SVD-based approach. Transform = U @ sqrt(S), then X = mean + Transform @ Z
where Z ~ N(0, I).
NumPy Compatibility: Produces 1-to-1 matching samples with NumPy for most common cases
(identity, diagonal, and correlated covariance matrices up to 4x4). For some larger matrices
(5x5+), the samples are statistically correct (same distribution) but may differ in exact
sequence due to differences in eigenvector sign conventions between Jacobi and LAPACK's
divide-and-conquer algorithms.
negative_binomial(double, double)
Draw a single sample from a negative binomial distribution.
public NDArray negative_binomial(double n, double p)
Parameters
Returns
negative_binomial(double, double, Shape)
Draw samples from a negative binomial distribution.
public NDArray negative_binomial(double n, double p, Shape size)
Parameters
ndoubleParameter of the distribution, > 0 (number of successes).
pdoubleParameter of the distribution, 0 < p <= 1 (probability of success).
sizeShapeOutput shape.
Returns
- NDArray
Drawn samples from the parameterized negative binomial distribution (integers >= 0).
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.negative_binomial.html
The negative binomial distribution models the number of failures before n successes,
where each trial has probability p of success.
For this distribution:
- mean = n * (1-p) / p
- variance = n * (1-p) / p^2
Uses gamma-Poisson mixture: Y ~ Gamma(n, (1-p)/p), X ~ Poisson(Y)
noncentral_chisquare(double, double)
Draw a single sample from a noncentral chi-square distribution.
public NDArray noncentral_chisquare(double df, double nonc)
Parameters
Returns
noncentral_chisquare(double, double, Shape)
Draw samples from a noncentral chi-square distribution.
public NDArray noncentral_chisquare(double df, double nonc, Shape size)
Parameters
dfdoubleDegrees of freedom, must be > 0.
noncdoubleNon-centrality parameter, must be >= 0.
sizeShapeOutput shape.
Returns
- NDArray
Drawn samples from the parameterized noncentral chi-square distribution.
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.noncentral_chisquare.html
The noncentral chi-square distribution is a generalization of the chi-square distribution.
Mean = df + nonc
noncentral_f(double, double, double)
Draw a single sample from the noncentral F distribution.
public NDArray noncentral_f(double dfnum, double dfden, double nonc)
Parameters
Returns
noncentral_f(double, double, double, Shape)
Draw samples from the noncentral F distribution.
public NDArray noncentral_f(double dfnum, double dfden, double nonc, Shape size)
Parameters
dfnumdoubleNumerator degrees of freedom, must be > 0.
dfdendoubleDenominator degrees of freedom, must be > 0.
noncdoubleNon-centrality parameter, must be >= 0.
sizeShapeOutput shape.
Returns
- NDArray
Drawn samples from the parameterized noncentral F distribution.
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.noncentral_f.html
When calculating the power of an experiment, the non-central F statistic
becomes important. When the null hypothesis is true, the F statistic follows
a central F distribution. When the null hypothesis is not true, it follows
a non-central F distribution.
normal(double, double)
Draw a single sample from a normal (Gaussian) distribution.
public NDArray normal(double loc = 0, double scale = 1)
Parameters
Returns
normal(double, double, Shape)
Draw random samples from a normal (Gaussian) distribution.
public NDArray normal(double loc, double scale, Shape size)
Parameters
locdoubleMean ("centre") of the distribution. Default is 0.
scaledoubleStandard deviation (spread or "width") of the distribution. Must be non-negative. Default is 1.
sizeShapeOutput shape.
Returns
- NDArray
Drawn samples from the parameterized normal distribution.
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.normal.html
The probability density function of the normal distribution, first derived
by De Moivre and 200 years later by both Gauss and Laplace independently,
is often called the bell curve because of its characteristic shape.
pareto(double)
Draw a single sample from a Pareto II or Lomax distribution.
public NDArray pareto(double a)
Parameters
adoubleShape of the distribution. Must be positive (> 0).
Returns
- NDArray
A single sample from the Pareto distribution as 0-d array.
pareto(double, Shape)
Draw samples from a Pareto II or Lomax distribution with specified shape.
public NDArray pareto(double a, Shape size)
Parameters
Returns
- NDArray
Drawn samples from the parameterized Pareto distribution.
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.pareto.html
NumPy's pareto returns samples from the Pareto II (Lomax) distribution,
not the classical Pareto distribution. The relationship is:
if Y ~ Pareto(a, m=1) then X = Y - 1 ~ Lomax(a).
The probability density function is:
f(x; a) = a / (1 + x)^(a+1) for x >= 0
The mean is 1/(a-1) for a > 1, undefined otherwise.
pareto(double, int)
Draw samples from a Pareto II or Lomax distribution with specified shape.
public NDArray pareto(double a, int size)
Parameters
Returns
- NDArray
Drawn samples from the parameterized Pareto distribution.
pareto(double, int[])
Draw samples from a Pareto II or Lomax distribution with specified shape.
public NDArray pareto(double a, int[] size)
Parameters
Returns
- NDArray
Drawn samples from the parameterized Pareto distribution.
pareto(double, long[])
Draw samples from a Pareto II or Lomax distribution with specified shape.
public NDArray pareto(double a, long[] size)
Parameters
Returns
- NDArray
Drawn samples from the parameterized Pareto distribution.
permutation(NDArray)
Randomly permute a sequence, or return a permuted range.
public NDArray permutation(NDArray x)
Parameters
xNDArrayIf x is an array, make a copy and shuffle the elements randomly.
Returns
- NDArray
Permuted sequence or array range.
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.permutation.html
If x is a multi-dimensional array, it is only shuffled along its first index.
permutation(int)
Randomly permute a sequence, or return a permuted range.
public NDArray permutation(int x)
Parameters
xintIf x is an integer, randomly permute np.arange(x).
Returns
- NDArray
Permuted sequence or array range.
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.permutation.html
If x is an integer, randomly permute np.arange(x).
If x is an array, make a copy and shuffle the elements randomly.
poisson(double)
Draw a single sample from a Poisson distribution.
public NDArray poisson(double lam = 1)
Parameters
lamdouble
Returns
poisson(double, Shape)
Draw samples from a Poisson distribution.
public NDArray poisson(double lam, Shape size)
Parameters
lamdoubleExpected number of events occurring in a fixed-time interval, must be >= 0. Default is 1.0.
sizeShapeOutput shape.
Returns
- NDArray
Drawn samples from the parameterized Poisson distribution.
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.poisson.html
The Poisson distribution is the limit of the binomial distribution for large N.
power(double)
Draw a single sample from a power distribution.
public NDArray power(double a)
Parameters
adoubleShape parameter of the distribution. Must be positive (> 0).
Returns
- NDArray
A single sample from the power distribution as 0-d array.
power(double, Shape)
Draws samples in [0, 1] from a power distribution with positive exponent a - 1.
public NDArray power(double a, Shape size)
Parameters
Returns
- NDArray
Drawn samples from the parameterized power distribution, in range [0, 1].
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.power.html
Also known as the power function distribution. The probability density function is:
P(x; a) = a * x^(a-1), for 0 <= x <= 1, a > 0.
The power function distribution is the inverse of the Pareto distribution.
It may also be seen as a special case of the Beta distribution.
power(double, int)
Draws samples in [0, 1] from a power distribution with positive exponent a - 1.
public NDArray power(double a, int size)
Parameters
adoubleShape parameter of the distribution. Must be positive (> 0).
sizeintOutput shape as single int.
Returns
- NDArray
Drawn samples from the parameterized power distribution, in range [0, 1].
power(double, int[])
Draws samples in [0, 1] from a power distribution with positive exponent a - 1.
public NDArray power(double a, int[] size)
Parameters
adoubleShape parameter of the distribution. Must be positive (> 0).
sizeint[]Output shape as int array.
Returns
- NDArray
Drawn samples from the parameterized power distribution, in range [0, 1].
power(double, long[])
Draws samples in [0, 1] from a power distribution with positive exponent a - 1.
public NDArray power(double a, long[] size)
Parameters
Returns
- NDArray
Drawn samples from the parameterized power distribution, in range [0, 1].
rand(Shape)
Random values in a given shape.
public NDArray rand(Shape shape)
Parameters
shapeShapeShape of the returned array.
Returns
- NDArray
Random values.
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.rand.html
Create an array of the given shape and populate it with random samples
from a uniform distribution over [0, 1).
rand(params long[])
Random values in a given shape.
public NDArray rand(params long[] shape)
Parameters
shapelong[]Dimensions of the returned array (d0, d1, ..., dn).
Returns
- NDArray
Random values.
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.rand.html
Create an array of the given shape and populate it with random samples
from a uniform distribution over [0, 1).
NumPy signature: rand(d0, d1, ..., dn) where d0..dn are dimension sizes.
randint(long, long, Shape, Type)
Return random integers from the "discrete uniform" distribution in the half-open interval [low, high).
public NDArray randint(long low, long high = -1, Shape size = default, Type dtype = null)
Parameters
lowlongLowest (signed) integer to be drawn from the distribution (unless high is not provided, in which case this parameter is one above the highest such integer).
highlongIf provided, one above the largest (signed) integer to be drawn from the distribution. If not provided (-1), results are from [0, low).
sizeShapeOutput shape. If None, a single value is returned.
dtypeTypeDesired dtype of the result. Default is np.int32.
Returns
- NDArray
Random integers from the appropriate distribution, or a single such random int if size not provided.
Remarks
randn(params long[])
Return a sample (or samples) from the "standard normal" distribution.
public NDArray randn(params long[] shape)
Parameters
shapelong[]Dimensions of the returned array (d0, d1, ..., dn).
Returns
- NDArray
Array of floating-point samples from the standard normal distribution.
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.randn.html
NumPy signature: randn(d0, d1, ..., dn) where d0..dn are dimension sizes.
For random samples from N(μ, σ²), use: σ * np.random.randn(...) + μ
randn<T>()
Return a scalar sample from the standard normal distribution.
public T randn<T>()
Returns
- T
A single random value.
Type Parameters
TThe desired output type.
random(params long[])
Return random floats in the half-open interval [0.0, 1.0).
public NDArray random(params long[] size)
Parameters
sizelong[]Output shape.
Returns
- NDArray
Array of random floats.
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.random.html
Alias for random_sample.
random_sample(params long[])
Return random floats in the half-open interval [0.0, 1.0).
public NDArray random_sample(params long[] size)
Parameters
sizelong[]Output shape.
Returns
- NDArray
Array of random floats of shape size.
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.random_sample.html
Results are from the "continuous uniform" distribution over the stated interval.
To sample Unif[a, b), b > a, multiply the output by (b-a) and add a.
rayleigh(double)
Draw a single sample from a Rayleigh distribution.
public NDArray rayleigh(double scale = 1)
Parameters
scaledouble
Returns
rayleigh(double, Shape)
Draw samples from a Rayleigh distribution.
public NDArray rayleigh(double scale, Shape size)
Parameters
scaledoubleScale parameter (also equals the mode). Must be non-negative. Default is 1.
sizeShapeOutput shape.
Returns
- NDArray
Drawn samples from the parameterized Rayleigh distribution.
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.rayleigh.html
The probability density function for the Rayleigh distribution is:
P(x; scale) = (x / scale^2) * exp(-x^2 / (2 * scale^2))
The Rayleigh distribution arises when the East and North components of wind
velocity have identical zero-mean Gaussian distributions. Then the wind speed
would have a Rayleigh distribution.
For Rayleigh(scale), mean = scale * sqrt(pi/2) ≈ 1.253 * scale
seed(int)
Seeds the generator with an int value. Validates that seed is non-negative (NumPy behavior).
public void seed(int seed)
Parameters
seedintSeed value in range [0, 2^31-1].
Remarks
NumPy accepts 0 to 2^32-1. Negative values throw: "Seed must be between 0 and 2**32 - 1"
Exceptions
- ValueError
If seed is negative.
seed(long)
Seeds the generator with a long value. Validates that seed is in range [0, 2^32-1] (NumPy behavior).
public void seed(long seed)
Parameters
seedlongSeed value in range [0, 2^32-1].
Exceptions
- ValueError
If seed is out of range.
seed(uint)
Seeds the generator with a uint value (full NumPy range). It can be called again to re-seed the generator.
public void seed(uint seed)
Parameters
seeduintSeed value in range [0, 2^32-1].
Remarks
This uses the MT19937 algorithm matching NumPy exactly. Same seed produces identical sequences to NumPy.
seed(uint[])
Seeds the generator with an array of uint values. Matches NumPy's init_by_array seeding.
public void seed(uint[] seed)
Parameters
seeduint[]Array of seed values.
seed(ulong)
Seeds the generator with a ulong value. Validates that seed is in range [0, 2^32-1] (NumPy behavior).
public void seed(ulong seed)
Parameters
seedulongSeed value in range [0, 2^32-1].
Exceptions
- ValueError
If seed is out of range.
set_state(NativeRandomState)
Set the internal state of the generator from a NativeRandomState. For use if one has reason to manually (re-)set the internal state of the pseudo-random number generating algorithm.
public void set_state(NativeRandomState state)
Parameters
stateNativeRandomStateThe state to restore onto this NumPyRandom
shuffle(NDArray)
Modify a sequence in-place by shuffling its contents.
public void shuffle(NDArray x)
Parameters
xNDArrayThe array or list to be shuffled.
Examples
// 1D array - elements are shuffled
var arr = np.arange(10);
np.random.shuffle(arr);
// 2D array - rows are shuffled, contents within rows unchanged
var arr2d = np.arange(9).reshape(3, 3);
np.random.shuffle(arr2d);
// e.g. [[6,7,8], [0,1,2], [3,4,5]] - rows reordered
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.shuffle.html
This function only shuffles the array along the first axis of a multi-dimensional array.
The order of sub-arrays is changed but their contents remain the same.
Note: NumPy's Generator API (rng.shuffle) supports an axis parameter, but the legacy
np.random.shuffle does not. This implementation matches the legacy API.
standard_cauchy()
Draw a single sample from a standard Cauchy distribution.
public NDArray standard_cauchy()
Returns
standard_cauchy(Shape)
Draw samples from a standard Cauchy distribution with mode = 0.
public NDArray standard_cauchy(Shape size)
Parameters
sizeShapeOutput shape.
Returns
- NDArray
Drawn samples from the standard Cauchy distribution.
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.standard_cauchy.html
Also known as the Lorentz distribution. The standard Cauchy distribution
has location parameter x0=0 and scale parameter gamma=1.
The Cauchy distribution has no defined mean or variance (infinite tails).
The median is 0, and the interquartile range is 2 (from -1 to 1).
Generated using inverse transform: X = tan(pi * (U - 0.5)) where U ~ Uniform(0, 1).
standard_exponential()
Draw a single sample from the standard exponential distribution.
public NDArray standard_exponential()
Returns
standard_exponential(Shape)
Draw samples from the standard exponential distribution.
public NDArray standard_exponential(Shape size)
Parameters
sizeShapeOutput shape.
Returns
- NDArray
Drawn samples from the standard exponential distribution (scale=1).
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.standard_exponential.html
The standard exponential distribution is the exponential distribution with scale=1.
It has mean=1 and variance=1.
Equivalent to: exponential(scale=1.0, size=size)
Uses inverse transform: X = -log(1 - U) where U ~ Uniform(0, 1)
standard_gamma(double)
Draw a single sample from a standard Gamma distribution.
public NDArray standard_gamma(double shape)
Parameters
shapedouble
Returns
standard_gamma(double, Shape)
Draw samples from a standard Gamma distribution (scale=1).
public NDArray standard_gamma(double shape, Shape size)
Parameters
Returns
- NDArray
Drawn samples from the standard gamma distribution.
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.standard_gamma.html
Samples are drawn from a Gamma distribution with shape parameter and scale=1.
For a different scale, multiply the result: scale * standard_gamma(shape).
The probability density function is:
p(x) = x^(shape-1) * e^(-x) / Gamma(shape)
standard_normal()
Draw a single sample from a standard Normal distribution.
public NDArray standard_normal()
Returns
standard_normal(Shape)
Draw samples from a standard Normal distribution (mean=0, stdev=1).
public NDArray standard_normal(Shape size)
Parameters
sizeShapeOutput shape.
Returns
- NDArray
A floating-point array of shape size of drawn samples.
Remarks
standard_t(double)
Draw a single sample from a standard Student's t distribution.
public NDArray standard_t(double df)
Parameters
dfdouble
Returns
standard_t(double, Shape)
Draw samples from a standard Student's t distribution with df degrees of freedom.
public NDArray standard_t(double df, Shape size)
Parameters
Returns
- NDArray
Drawn samples from the parameterized standard Student's t distribution.
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.standard_t.html
A special case of the hyperbolic distribution. As df gets large, the result
resembles that of the standard normal distribution.
The probability density function is:
P(x, df) = Gamma((df+1)/2) / (sqrt(pi*df) * Gamma(df/2)) * (1 + x^2/df)^(-(df+1)/2)
triangular(double, double, double)
Draw a single sample from the triangular distribution.
public NDArray triangular(double left, double mode, double right)
Parameters
Returns
triangular(double, double, double, Shape)
Draw samples from the triangular distribution over the interval [left, right].
public NDArray triangular(double left, double mode, double right, Shape size)
Parameters
leftdoubleLower limit.
modedoubleThe value where the peak of the distribution occurs (left <= mode <= right).
rightdoubleUpper limit, must be larger than left.
sizeShapeOutput shape.
Returns
- NDArray
Drawn samples from the parameterized triangular distribution.
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.triangular.html
The triangular distribution is a continuous probability distribution with lower limit left,
peak at mode, and upper limit right.
uniform(NDArray, NDArray, Type)
Draw samples from a uniform distribution with array boundaries.
public NDArray uniform(NDArray low, NDArray high, Type dtype = null)
Parameters
lowNDArrayLower boundary array.
highNDArrayUpper boundary array.
dtypeTypeThe dtype of the output NDArray.
Returns
- NDArray
Drawn samples.
uniform(double, double)
Draw a single sample from a uniform distribution.
public NDArray uniform(double low = 0, double high = 1)
Parameters
Returns
uniform(double, double, Shape)
Draw samples from a uniform distribution.
public NDArray uniform(double low, double high, Shape size)
Parameters
lowdoubleLower boundary of the output interval. All values generated will be >= low. Default is 0.
highdoubleUpper boundary of the output interval. All values generated will be < high. Default is 1.0.
sizeShapeOutput shape.
Returns
- NDArray
Drawn samples from the parameterized uniform distribution.
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.uniform.html
Samples are uniformly distributed over the half-open interval [low, high)
(includes low, but excludes high). In other words, any value within the
given interval is equally likely to be drawn by uniform.
vonmises(double, double)
Draw a single sample from a von Mises distribution.
public NDArray vonmises(double mu, double kappa)
Parameters
Returns
vonmises(double, double, Shape)
Draw samples from a von Mises distribution.
public NDArray vonmises(double mu, double kappa, Shape size)
Parameters
mudoubleMode ("center") of the distribution in radians.
kappadoubleConcentration parameter of the distribution. Must be >= 0. When kappa = 0, the distribution is uniform on the circle. As kappa increases, the distribution becomes more concentrated around mu.
sizeShapeOutput shape.
Returns
- NDArray
Drawn samples from the parameterized von Mises distribution.
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.vonmises.html
The von Mises distribution (also known as the circular normal distribution)
is a continuous probability distribution on the unit circle. It may be thought
of as the circular analogue of the normal distribution.
Samples are drawn on the interval [-pi, pi].
The probability density function is:
p(x) = exp(kappa * cos(x - mu)) / (2 * pi * I_0(kappa))
where I_0(kappa) is the modified Bessel function of order 0.
Exceptions
- ArgumentException
If kappa is negative.
wald(double, double)
Draw a single sample from a Wald distribution.
public NDArray wald(double mean, double scale)
Parameters
Returns
wald(double, double, Shape)
Draw samples from a Wald, or inverse Gaussian, distribution.
public NDArray wald(double mean, double scale, Shape size)
Parameters
meandoubleDistribution mean, must be > 0.
scaledoubleScale parameter, must be > 0.
sizeShapeOutput shape.
Returns
- NDArray
Drawn samples from the parameterized Wald distribution.
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.wald.html
The inverse Gaussian distribution was first studied in relationship to
Brownian motion. As the scale approaches infinity, the distribution
becomes more like a Gaussian.
The probability density function is:
P(x;mean,scale) = sqrt(scale/(2pix^3)) * exp(-scale*(x-mean)^2 / (2mean^2x))
weibull(double)
Draw a single sample from a Weibull distribution.
public NDArray weibull(double a)
Parameters
adouble
Returns
weibull(double, Shape)
Draw samples from a Weibull distribution.
public NDArray weibull(double a, Shape size)
Parameters
Returns
- NDArray
Drawn samples from the Weibull distribution.
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.weibull.html
The Weibull distribution is a continuous probability distribution with
probability density function: f(x;a) = a * x^(a-1) * exp(-x^a) for x >= 0.
This is the standard Weibull with scale=1. For Weibull with scale parameter,
use: scale * np.random.weibull(a, size).
When a=1, the Weibull distribution reduces to the exponential distribution.
Exceptions
- ArgumentException
If a is negative.
zipf(double)
Draw a single sample from a Zipf distribution.
public NDArray zipf(double a)
Parameters
adouble
Returns
zipf(double, Shape)
Draw samples from a Zipf distribution.
public NDArray zipf(double a, Shape size)
Parameters
Returns
- NDArray
Drawn samples from the parameterized Zipf distribution as int64.
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.zipf.html
The Zipf distribution (also known as the zeta distribution) is a discrete
distribution commonly used to model the frequency of words in texts, the
size of cities, and many other phenomena.
The probability mass function is:
p(k) = k^(-a) / zeta(a)
where k >= 1 and zeta(a) is the Riemann zeta function.
Samples are positive integers.