Table of Contents

Class Generator

Namespace
NumSharp
Assembly
NumSharp.dll

The modern NumPy random number container returned by np.random.default_rng.

public sealed class Generator
Inheritance
Generator
Inherited Members

Remarks

Port of NumPy 2.4.2's numpy.random.Generator (numpy/random/_generator.pyx). Unlike the legacy NumPyRandom (RandomState, MT19937 + polar-method normal / inverse-CDF exponential / masked bounded integers), Generator draws from a PCG64 bit generator and uses NumPy's newer algorithms — ziggurat normal/exponential and Lemire bounded integers — so its stream matches default_rng(seed) bit-for-bit, not RandomState.

Constructors

Generator(BitGenerator)

Constructs a Generator over the given bit generator.

public Generator(BitGenerator bitGenerator)

Parameters

bitGenerator BitGenerator

Properties

bit_generator

The bit generator supplying this Generator's stream.

public BitGenerator bit_generator { get; }

Property Value

BitGenerator

Methods

ToString()

Returns a string that represents the current object.

public override string ToString()

Returns

string

A string that represents the current object.

bytes(long)

Return random bytes.

public NDArray<byte> bytes(long length)

Parameters

length long

Number of random bytes.

Returns

NDArray<byte>

A 1-D NDArray<TDType> of byte (dtype uint8), length length — the NumSharp analogue of NumPy's bytes object.

Remarks

https://numpy.org/doc/stable/reference/random/generated/numpy.random.Generator.bytes.html
Byte-identical to NumPy: draws ceil(length/4) uint32 words from PCG64 (via the 32-bit buffered path), packs them little-endian, and truncates to length. As with bytes(long), the result is an unmanaged-backed NDArray<TDType>, so — matching NumPy's 64-bit npy_intp length — it is NOT capped at MaxLength and a request over 2 GiB still succeeds.

choice(NDArray, Shape?, bool, NDArray, int, bool)

Generates a random sample from a given array (or arange(a) when a is an integer population size).

public NDArray choice(NDArray a, Shape? size = null, bool replace = true, NDArray p = null, int axis = 0, bool shuffle = true)

Parameters

a NDArray
size Shape?
replace bool
p NDArray
axis int
shuffle bool

Returns

NDArray

Remarks

https://numpy.org/doc/stable/reference/random/generated/numpy.random.Generator.choice.html
Byte-identical to NumPy for the common paths: with-replacement (uniform or p-weighted) and without-replacement uniform (Floyd's algorithm + optional shuffle). Without-replacement WITH weights is not yet ported.

choice(long, Shape?, bool, NDArray, bool)

choice(int population, ...) convenience: draws from arange(a).

public NDArray choice(long a, Shape? size = null, bool replace = true, NDArray p = null, bool shuffle = true)

Parameters

a long
size Shape?
replace bool
p NDArray
shuffle bool

Returns

NDArray

exponential(double, Shape)

Draw samples from an exponential distribution.

public NDArray exponential(double scale = 1, Shape size = default)

Parameters

scale double

The scale parameter (1/rate). Must be non-negative.

size Shape

Output shape.

Returns

NDArray

Remarks

https://numpy.org/doc/stable/reference/random/generated/numpy.random.Generator.exponential.html
scale * standard_exponential() (ziggurat), byte-identical to NumPy.

gamma(double, double, Shape)

Draw samples from a Gamma distribution.

public NDArray gamma(double shape, double scale = 1, Shape size = default)

Parameters

shape double

The shape parameter (must be non-negative).

scale double

The scale parameter (must be non-negative). Default 1.

size Shape

Output shape.

Returns

NDArray

Remarks

integers(long, long?, Shape, DType, bool)

Return random integers from low (inclusive) to high (exclusive, or inclusive when endpoint is true).

public NDArray integers(long low, long? high = null, Shape size = default, DType dtype = null, bool endpoint = false)

Parameters

low long

Lowest integer drawn (or the highest, one above, when high is null).

high long?

If provided, one above the largest integer drawn (or the largest when endpoint).

size Shape

Output shape. If default/scalar a single value is returned.

dtype DType

Desired integer dtype. Default is int64.

endpoint bool

If true, sample from the closed interval [low, high].

Returns

NDArray

Remarks

https://numpy.org/doc/stable/reference/random/generated/numpy.random.Generator.integers.html
Uses Lemire's method (NumPy's Generator default, use_masked=False) — NOT the legacy masked rejection of RandomState.randint — so the stream is byte-identical to default_rng(seed).integers(...).

integers(ulong, ulong?, Shape, DType, bool)

Unsigned overload of integers(long, long?, Shape, DType, bool) — the only way to reach the upper half of the uint64 range (values above MaxValue), which NumPy addresses with arbitrary-precision Python ints. The full [0, 2**64) range is drawn as integers(0UL, ulong.MaxValue, dtype: np.uint64, endpoint: true).

public NDArray integers(ulong low, ulong? high = null, Shape size = default, DType dtype = null, bool endpoint = false)

Parameters

low ulong
high ulong?
size Shape
dtype DType
endpoint bool

Returns

NDArray

Remarks

Anything expressible in the signed domain is forwarded verbatim to the signed overload, so only genuinely-large uint64 requests take the dedicated path — which, like NumPy, rejects a non-uint64 dtype whose range cannot hold the requested high (high is out of bounds…).

normal(double, double, Shape)

Draw samples from a normal (Gaussian) distribution.

public NDArray normal(double loc = 0, double scale = 1, Shape size = default)

Parameters

loc double

Mean of the distribution.

scale double

Standard deviation (must be non-negative).

size Shape

Output shape.

Returns

NDArray

Remarks

permutation(NDArray, int)

Randomly permute a sequence, or return a permuted range.

public NDArray permutation(NDArray x, int axis = 0)

Parameters

x NDArray
axis int

Returns

NDArray

Remarks

permutation(long)

Randomly permute a sequence, or return a permuted range.

public NDArray permutation(long x)

Parameters

x long

Returns

NDArray

Remarks

permuted(NDArray, int?, NDArray)

Randomly permute x along axis. Unlike shuffle(NDArray, int), each slice along the axis is shuffled INDEPENDENTLY of the others.

public NDArray permuted(NDArray x, int? axis = null, NDArray @out = null)

Parameters

x NDArray

Array to shuffle (at least 1-D when an axis is given).

axis int?

Axis whose slices are each shuffled; null shuffles the flattened array.

out NDArray

Optional destination (must match x's shape); returned when given.

Returns

NDArray

Remarks

https://numpy.org/doc/stable/reference/random/generated/numpy.random.Generator.permuted.html
Byte-identical to NumPy: axis=None shuffles the C-order flattened copy; an explicit axis runs an independent random_interval Fisher–Yates over each 1-D slice, iterating the remaining axes in C-order (NumPy's PyArray_IterAllButAxis).

random(Shape, DType, NDArray)

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

public NDArray random(Shape size = default, DType dtype = null, NDArray @out = null)

Parameters

size Shape

Output shape. If default/scalar a single value is returned.

dtype DType

Desired dtype — only float64 (default) and float32 are supported.

out NDArray

Optional output array to place the result in.

Returns

NDArray

Remarks

shuffle(NDArray, int)

Modify an array in-place by shuffling its contents along the given axis.

public void shuffle(NDArray x, int axis = 0)

Parameters

x NDArray
axis int

Remarks

https://numpy.org/doc/stable/reference/random/generated/numpy.random.Generator.shuffle.html
Fisher–Yates using random_interval (mask-rejection), byte-identical to NumPy.

standard_exponential(Shape, DType, string, NDArray)

Draw samples from the standard exponential distribution.

public NDArray standard_exponential(Shape size = default, DType dtype = null, string method = "zig", NDArray @out = null)

Parameters

size Shape

Output shape.

dtype DType

float64 (default) or float32.

method string

Either "zig" (ziggurat, default) or "inv" (inverse CDF).

out NDArray

Optional output array.

Returns

NDArray

Remarks

standard_gamma(double, Shape, DType, NDArray)

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

public NDArray standard_gamma(double shape, Shape size = default, DType dtype = null, NDArray @out = null)

Parameters

shape double

The shape parameter (must be non-negative).

size Shape

Output shape.

dtype DType

float64 (default) or float32.

out NDArray

Optional output array.

Returns

NDArray

Remarks

standard_normal(Shape, DType, NDArray)

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

public NDArray standard_normal(Shape size = default, DType dtype = null, NDArray @out = null)

Parameters

size Shape

Output shape. If default/scalar a single value is returned.

dtype DType

Desired dtype — float64 (default) or float32.

out NDArray

Optional output array.

Returns

NDArray

Remarks

https://numpy.org/doc/stable/reference/random/generated/numpy.random.Generator.standard_normal.html
Uses NumPy's ziggurat sampler, so the stream matches default_rng(seed).standard_normal(...).

uniform(double, double, Shape)

Draw samples from a uniform distribution over [low, high).

public NDArray uniform(double low = 0, double high = 1, Shape size = default)

Parameters

low double

Lower boundary (inclusive). Default 0.

high double

Upper boundary (exclusive). Default 1.

size Shape

Output shape.

Returns

NDArray

Remarks

https://numpy.org/doc/stable/reference/random/generated/numpy.random.Generator.uniform.html
low + (high - low) * next_double(), byte-identical to NumPy.