Table of Contents

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

bitGenerator MT19937

NumPyRandom(NativeRandomState)

protected NumPyRandom(NativeRandomState nativeRandomState)

Parameters

nativeRandomState NativeRandomState

NumPyRandom(int)

protected NumPyRandom(int seed)

Parameters

seed int

Fields

randomizer

The MT19937 bit generator (NumPy-compatible).

protected MT19937 randomizer

Field Value

MT19937

Properties

Seed

public int Seed { get; set; }

Property Value

int

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

double

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

NumPyRandom

RandomState(NativeRandomState)

Returns a new instance of NumPyRandom.

public NumPyRandom RandomState(NativeRandomState state)

Parameters

state NativeRandomState

Returns

NumPyRandom

RandomState(int)

Returns a new instance of NumPyRandom.

public NumPyRandom RandomState(int seed)

Parameters

seed int

Returns

NumPyRandom

bernoulli(double)

Draw a single sample from a Bernoulli distribution.

public NDArray bernoulli(double p)

Parameters

p double

Probability 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

p double

Probability of success (1), must be in [0, 1].

size Shape

Output shape.

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

a double
b double

Returns

NDArray

beta(double, double, Shape)

Draw samples from a Beta distribution.

public NDArray beta(double a, double b, Shape size)

Parameters

a double

Alpha (α), positive (>0).

b double

Beta (β), positive (>0).

size Shape

Output shape.

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

n int
p double

Returns

NDArray

binomial(int, double, Shape)

Draw samples from a binomial distribution.

public NDArray binomial(int n, double p, Shape size)

Parameters

n int

Parameter of the distribution, >= 0. Number of trials.

p double

Parameter of the distribution, >= 0 and <= 1. Probability of success.

size Shape

Output 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

df double

Returns

NDArray

chisquare(double, Shape)

Draw samples from a chi-square distribution.

public NDArray chisquare(double df, Shape size)

Parameters

df double

Number of degrees of freedom, must be > 0.

size Shape

Output shape.

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

a NDArray

Array to sample from.

size Shape

Output shape. Default is None, in which case a single value is returned.

replace bool

Whether the sample is with or without replacement. Default is True.

p double[]

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

a int

If an int, the random sample is generated from np.arange(a).

size Shape

Output shape. Default is None, in which case a single value is returned.

replace bool

Whether the sample is with or without replacement. Default is True.

p double[]

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

a long

If a long, the random sample is generated from np.arange(a).

size Shape

Output shape. Default is None, in which case a single value is returned.

replace bool

Whether the sample is with or without replacement. Default is True.

p double[]

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

alpha NDArray

Concentration parameters as NDArray.

size Shape?

Output shape.

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

alpha double[]

Concentration parameters.

size int

Number of samples to draw.

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

alpha double[]

Concentration parameters.

size int[]

Output shape as int array.

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

alpha double[]

Concentration parameters.

size long[]

Output shape.

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

alpha double[]

Concentration parameters of the distribution (k > 0 elements, each > 0).

size Shape?

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

scale double

Returns

NDArray

exponential(double, Shape)

Draw samples from an exponential distribution.

public NDArray exponential(double scale, Shape size)

Parameters

scale double

The scale parameter, β = 1/λ. Must be non-negative. Default is 1.0.

size Shape

Output 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

dfnum double
dfden double

Returns

NDArray

f(double, double, Shape)

public NDArray f(double dfnum, double dfden, Shape size)

Parameters

dfnum double
dfden double
size Shape

Returns

NDArray

gamma(double, double)

Draw a single sample from a Gamma distribution.

public NDArray gamma(double shape, double scale = 1)

Parameters

shape double
scale double

Returns

NDArray

gamma(double, double, Shape)

Draw samples from a Gamma distribution.

public NDArray gamma(double shape, double scale, Shape size)

Parameters

shape double

The shape of the gamma distribution. Must be non-negative.

scale double

The scale of the gamma distribution. Must be non-negative. Default is 1.0.

size Shape

Output 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

p double

Returns

NDArray

geometric(double, Shape)

Draw samples from the geometric distribution.

public NDArray geometric(double p, Shape size)

Parameters

p double

The probability of success of an individual trial.

size Shape

Output shape.

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

loc double
scale double

Returns

NDArray

gumbel(double, double, Shape)

Draw samples from a Gumbel distribution (extreme value type I).

public NDArray gumbel(double loc, double scale, Shape size)

Parameters

loc double

The location of the mode of the distribution. Default is 0.

scale double

The scale parameter of the distribution. Must be non-negative. Default is 1.

size Shape

Output 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

ngood long

Number of ways to make a good selection. Must be non-negative.

nbad long

Number of ways to make a bad selection. Must be non-negative.

nsample long

Number 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

ngood long

Number of ways to make a good selection. Must be non-negative.

nbad long

Number of ways to make a bad selection. Must be non-negative.

nsample long

Number of items sampled. Must be >= 1 and <= ngood + nbad.

size int

Output 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

ngood long

Number of ways to make a good selection. Must be non-negative.

nbad long

Number of ways to make a bad selection. Must be non-negative.

nsample long

Number of items sampled. Must be >= 1 and <= ngood + nbad.

size int[]

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

ngood long

Number of ways to make a good selection. Must be non-negative.

nbad long

Number of ways to make a bad selection. Must be non-negative.

nsample long

Number of items sampled. Must be >= 1 and <= ngood + nbad.

size long[]

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

ngood long

Number of ways to make a good selection. Must be non-negative.

nbad long

Number of ways to make a bad selection. Must be non-negative.

nsample long

Number of items sampled. Must be >= 1 and <= ngood + nbad.

size Shape?

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

loc double
scale double

Returns

NDArray

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

loc double

The position of the distribution peak. Default is 0.

scale double

The exponential decay. Must be non-negative. Default is 1.

size Shape

Output 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

loc double
scale double

Returns

NDArray

logistic(double, double, Shape)

Draw samples from a logistic distribution.

public NDArray logistic(double loc, double scale, Shape size)

Parameters

loc double

Mean of the distribution. Default is 0.

scale double

Scale parameter (must be >= 0). Default is 1.

size Shape

Output 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

mean double
sigma double

Returns

NDArray

lognormal(double, double, Shape)

Draw samples from a log-normal distribution.

public NDArray lognormal(double mean, double sigma, Shape size)

Parameters

mean double

Mean value of the underlying normal distribution. Default is 0.

sigma double

Standard deviation of the underlying normal distribution. Must be non-negative. Default is 1.

size Shape

Output 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

p double

Shape parameter for the distribution. Must be in the range [0, 1).

size Shape

Output 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

p double

Shape parameter for the distribution. Must be in the range [0, 1).

size int

Output 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

p double

Shape parameter for the distribution. Must be in the range [0, 1).

size int[]

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

p double

Shape parameter for the distribution. Must be in the range [0, 1).

size long[]

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

p double

Shape parameter for the distribution. Must be in the range [0, 1).

size Shape?

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

n int

Number of experiments (>= 0).

pvals double[]

Probabilities of each of the k different outcomes. Must sum to ~1.

size int

Number 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

n int

Number of experiments (>= 0).

pvals double[]

Probabilities of each of the k different outcomes. Must sum to ~1.

size int[]

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

n int

Number of experiments (>= 0).

pvals double[]

Probabilities of each of the k different outcomes. Must sum to ~1.

size Shape?

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

mean NDArray
cov NDArray
size Shape?
check_valid string
tol double

Returns

NDArray

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

mean double[]
cov double[,]
size int
check_valid string
tol double

Returns

NDArray

multivariate_normal(double[], double[,], int[])

Draw random samples from a multivariate normal distribution.

public NDArray multivariate_normal(double[] mean, double[,] cov, int[] size)

Parameters

mean double[]
cov double[,]
size int[]

Returns

NDArray

multivariate_normal(double[], double[,], long[])

Draw random samples from a multivariate normal distribution.

public NDArray multivariate_normal(double[] mean, double[,] cov, long[] size)

Parameters

mean double[]
cov double[,]
size long[]

Returns

NDArray

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

mean double[]

Mean of the N-dimensional distribution (1D array of length N).

cov double[,]

Covariance matrix of the distribution (N x N symmetric positive-semidefinite).

size Shape?

Output shape. Given a shape of (m, n, k), mnk samples are generated with output shape (m, n, k, N).

check_valid string

Behavior when the covariance matrix is not positive semidefinite: "warn", "raise", or "ignore".

tol double

Tolerance 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

n double
p double

Returns

NDArray

negative_binomial(double, double, Shape)

Draw samples from a negative binomial distribution.

public NDArray negative_binomial(double n, double p, Shape size)

Parameters

n double

Parameter of the distribution, > 0 (number of successes).

p double

Parameter of the distribution, 0 < p <= 1 (probability of success).

size Shape

Output 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

df double
nonc double

Returns

NDArray

noncentral_chisquare(double, double, Shape)

Draw samples from a noncentral chi-square distribution.

public NDArray noncentral_chisquare(double df, double nonc, Shape size)

Parameters

df double

Degrees of freedom, must be > 0.

nonc double

Non-centrality parameter, must be >= 0.

size Shape

Output 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

dfnum double
dfden double
nonc double

Returns

NDArray

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

dfnum double

Numerator degrees of freedom, must be > 0.

dfden double

Denominator degrees of freedom, must be > 0.

nonc double

Non-centrality parameter, must be >= 0.

size Shape

Output 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

loc double
scale double

Returns

NDArray

normal(double, double, Shape)

Draw random samples from a normal (Gaussian) distribution.

public NDArray normal(double loc, double scale, Shape size)

Parameters

loc double

Mean ("centre") of the distribution. Default is 0.

scale double

Standard deviation (spread or "width") of the distribution. Must be non-negative. Default is 1.

size Shape

Output 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

a double

Shape 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

a double

Shape of the distribution. Must be positive (> 0).

size Shape

Output shape.

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

a double

Shape of the distribution. Must be positive (> 0).

size int

Output shape as single int.

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

a double

Shape of the distribution. Must be positive (> 0).

size int[]

Output shape as int array.

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

a double

Shape of the distribution. Must be positive (> 0).

size long[]

Output shape.

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

x NDArray

If 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

x int

If 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

lam double

Returns

NDArray

poisson(double, Shape)

Draw samples from a Poisson distribution.

public NDArray poisson(double lam, Shape size)

Parameters

lam double

Expected number of events occurring in a fixed-time interval, must be >= 0. Default is 1.0.

size Shape

Output 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

a double

Shape 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

a double

Shape parameter of the distribution. Must be positive (> 0).

size Shape

Output shape.

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

a double

Shape parameter of the distribution. Must be positive (> 0).

size int

Output 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

a double

Shape parameter of the distribution. Must be positive (> 0).

size int[]

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

a double

Shape parameter of the distribution. Must be positive (> 0).

size long[]

Output shape.

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

shape Shape

Shape 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

shape long[]

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

low long

Lowest (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).

high long

If provided, one above the largest (signed) integer to be drawn from the distribution. If not provided (-1), results are from [0, low).

size Shape

Output shape. If None, a single value is returned.

dtype Type

Desired 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

shape long[]

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

T

The 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

size long[]

Output shape.

Returns

NDArray

Array of random floats.

Remarks

random_sample(params long[])

Return random floats in the half-open interval [0.0, 1.0).

public NDArray random_sample(params long[] size)

Parameters

size long[]

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

scale double

Returns

NDArray

rayleigh(double, Shape)

Draw samples from a Rayleigh distribution.

public NDArray rayleigh(double scale, Shape size)

Parameters

scale double

Scale parameter (also equals the mode). Must be non-negative. Default is 1.

size Shape

Output 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

seed int

Seed 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

seed long

Seed 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

seed uint

Seed 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

seed uint[]

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

seed ulong

Seed 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

state NativeRandomState

The state to restore onto this NumPyRandom

shuffle(NDArray)

Modify a sequence in-place by shuffling its contents.

public void shuffle(NDArray x)

Parameters

x NDArray

The 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

NDArray

standard_cauchy(Shape)

Draw samples from a standard Cauchy distribution with mode = 0.

public NDArray standard_cauchy(Shape size)

Parameters

size Shape

Output 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

NDArray

standard_exponential(Shape)

Draw samples from the standard exponential distribution.

public NDArray standard_exponential(Shape size)

Parameters

size Shape

Output 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

shape double

Returns

NDArray

standard_gamma(double, Shape)

Draw samples from a standard Gamma distribution (scale=1).

public NDArray standard_gamma(double shape, Shape size)

Parameters

shape double

The shape of the gamma distribution. Must be >= 0.

size Shape

Output shape.

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

NDArray

standard_normal(Shape)

Draw samples from a standard Normal distribution (mean=0, stdev=1).

public NDArray standard_normal(Shape size)

Parameters

size Shape

Output 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

df double

Returns

NDArray

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

df double

Degrees of freedom, must be > 0.

size Shape

Output shape.

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

left double
mode double
right double

Returns

NDArray

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

left double

Lower limit.

mode double

The value where the peak of the distribution occurs (left <= mode <= right).

right double

Upper limit, must be larger than left.

size Shape

Output 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

low NDArray

Lower boundary array.

high NDArray

Upper boundary array.

dtype Type

The 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

low double
high double

Returns

NDArray

uniform(double, double, Shape)

Draw samples from a uniform distribution.

public NDArray uniform(double low, double high, Shape size)

Parameters

low double

Lower boundary of the output interval. All values generated will be >= low. Default is 0.

high double

Upper boundary of the output interval. All values generated will be < high. Default is 1.0.

size Shape

Output 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

mu double
kappa double

Returns

NDArray

vonmises(double, double, Shape)

Draw samples from a von Mises distribution.

public NDArray vonmises(double mu, double kappa, Shape size)

Parameters

mu double

Mode ("center") of the distribution in radians.

kappa double

Concentration 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.

size Shape

Output 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

mean double
scale double

Returns

NDArray

wald(double, double, Shape)

Draw samples from a Wald, or inverse Gaussian, distribution.

public NDArray wald(double mean, double scale, Shape size)

Parameters

mean double

Distribution mean, must be > 0.

scale double

Scale parameter, must be > 0.

size Shape

Output 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

a double

Returns

NDArray

weibull(double, Shape)

Draw samples from a Weibull distribution.

public NDArray weibull(double a, Shape size)

Parameters

a double

Shape parameter of the distribution. Must be non-negative.

size Shape

Output shape.

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

a double

Returns

NDArray

zipf(double, Shape)

Draw samples from a Zipf distribution.

public NDArray zipf(double a, Shape size)

Parameters

a double

Distribution parameter. Must be greater than 1.

size Shape

Output shape.

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.