Table of Contents

Class NumPyRandom

Namespace
NumSharp
Assembly
NumSharp.dll

A class that serves as numpy.random.RandomState in python.

public class NumPyRandom
Inheritance
NumPyRandom
Inherited Members
Extension Methods

Remarks

Constructors

NumPyRandom()

protected NumPyRandom()

NumPyRandom(NativeRandomState)

protected NumPyRandom(NativeRandomState nativeRandomState)

Parameters

nativeRandomState NativeRandomState

NumPyRandom(Randomizer)

protected NumPyRandom(Randomizer randomizer)

Parameters

randomizer Randomizer

NumPyRandom(int)

protected NumPyRandom(int seed)

Parameters

seed int

Fields

randomizer

protected Randomizer randomizer

Field Value

Randomizer

Properties

Seed

public int Seed { get; set; }

Property Value

int

Methods

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

bernoulli(double, params int[])

Draw samples from a Bernoulli distribution.

public NDArray bernoulli(double p, params int[] size)

Parameters

p double

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

size int[]

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

beta(double, double, params int[])

Draw samples from a Beta distribution.

public NDArray beta(double a, double b, params int[] size)

Parameters

a double

Alpha (α), positive (>0).

b double

Beta (β), positive (>0).

size int[]

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, 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].

binomial(int, double, params int[])

Draw samples from a binomial distribution.

public NDArray binomial(int n, double p, params int[] 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 int[]

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

chisquare(double, params int[])

Draw samples from a chi-square distribution.

public NDArray chisquare(double df, params int[] size)

Parameters

df double

Number of degrees of freedom, must be > 0.

size int[]

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

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.

exponential(double, params int[])

Draw samples from an exponential distribution.

public NDArray exponential(double scale, params int[] size)

Parameters

scale double

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

size int[]

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.

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.

gamma(double, double, params int[])

Draw samples from a Gamma distribution.

public NDArray gamma(double shape, double scale, params int[] 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 int[]

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

geometric(double, params int[])

Draw samples from the geometric distribution.

public NDArray geometric(double p, params int[] size)

Parameters

p double

The probability of success of an individual trial.

size int[]

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 NumPyRandom representing the internal state of the generator.

public NativeRandomState get_state()

Returns

NativeRandomState

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.

lognormal(double, double, params int[])

Draw samples from a log-normal distribution.

public NDArray lognormal(double mean, double sigma, params int[] 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 int[]

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.

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.

normal(double, double, params int[])

Draw random samples from a normal (Gaussian) distribution.

public NDArray normal(double loc, double scale, params int[] 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 int[]

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.

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

poisson(double, params int[])

Draw samples from a Poisson distribution.

public NDArray poisson(double lam, params int[] size)

Parameters

lam double

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

size int[]

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.

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 int[])

Random values in a given shape.

public NDArray rand(params int[] shape)

Parameters

shape int[]

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)

public NDArray randint(long low, long high = -1, Shape size = default, Type dtype = null)

Parameters

low long
high long
size Shape
dtype Type

Returns

NDArray

randn(Shape)

Return a sample (or samples) from the "standard normal" distribution.

public NDArray randn(Shape shape)

Parameters

shape Shape

Output shape.

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(params int[])

Return a sample (or samples) from the "standard normal" distribution.

public NDArray randn(params int[] shape)

Parameters

shape int[]

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(Shape)

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

public NDArray random(Shape size)

Parameters

size Shape

Output shape.

Returns

NDArray

Array of random floats.

Remarks

random(params int[])

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

public NDArray random(params int[] size)

Parameters

size int[]

Output shape.

Returns

NDArray

Array of random floats.

Remarks

random_sample(Shape)

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

public NDArray random_sample(Shape size)

Parameters

size Shape

Output shape.

Returns

NDArray

Array of random floats.

Remarks

random_sample(params int[])

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

public NDArray random_sample(params int[] size)

Parameters

size int[]

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.

seed(int)

Seeds the generator. It can be called again to re-seed the generator.

public void seed(int seed)

Parameters

seed int

set_state(NativeRandomState)

Set the internal state of the generator from a NumPyRandom. 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 nativeRandomState)

Parameters

nativeRandomState 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_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_normal(params int[])

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

public NDArray standard_normal(params int[] size)

Parameters

size int[]

Output shape.

Returns

NDArray

A floating-point array of shape size of drawn samples.

Remarks

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

uniform(double, double, params int[])

Draw samples from a uniform distribution.

public NDArray uniform(double low, double high, params int[] 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 int[]

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.