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
bitGeneratorBitGenerator
Properties
bit_generator
The bit generator supplying this Generator's stream.
public BitGenerator bit_generator { get; }
Property Value
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
lengthlongNumber of random bytes.
Returns
- NDArray<byte>
A 1-D NDArray<TDType> of byte (dtype
uint8), lengthlength— the NumSharp analogue of NumPy'sbytesobject.
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
Returns
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
Returns
exponential(double, Shape)
Draw samples from an exponential distribution.
public NDArray exponential(double scale = 1, Shape size = default)
Parameters
Returns
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
shapedoubleThe shape parameter (must be non-negative).
scaledoubleThe scale parameter (must be non-negative). Default 1.
sizeShapeOutput shape.
Returns
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.Generator.gamma.html
scale * standard_gamma(shape), byte-identical to NumPy.
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
lowlongLowest integer drawn (or the highest, one above, when
highis null).highlong?If provided, one above the largest integer drawn (or the largest when
endpoint).sizeShapeOutput shape. If default/scalar a single value is returned.
dtypeDTypeDesired integer dtype. Default is int64.
endpointboolIf true, sample from the closed interval
[low, high].
Returns
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
Returns
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
locdoubleMean of the distribution.
scaledoubleStandard deviation (must be non-negative).
sizeShapeOutput shape.
Returns
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.Generator.normal.html
loc + scale * standard_normal(), byte-identical to NumPy.
permutation(NDArray, int)
Randomly permute a sequence, or return a permuted range.
public NDArray permutation(NDArray x, int axis = 0)
Parameters
Returns
Remarks
permutation(long)
Randomly permute a sequence, or return a permuted range.
public NDArray permutation(long x)
Parameters
xlong
Returns
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
xNDArrayArray to shuffle (at least 1-D when an axis is given).
axisint?Axis whose slices are each shuffled;
nullshuffles the flattened array.outNDArrayOptional destination (must match
x's shape); returned when given.
Returns
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
sizeShapeOutput shape. If default/scalar a single value is returned.
dtypeDTypeDesired dtype — only
float64(default) andfloat32are supported.outNDArrayOptional output array to place the result in.
Returns
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
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
sizeShapeOutput shape.
dtypeDTypefloat64(default) orfloat32.methodstringEither
"zig"(ziggurat, default) or"inv"(inverse CDF).outNDArrayOptional output array.
Returns
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
shapedoubleThe shape parameter (must be non-negative).
sizeShapeOutput shape.
dtypeDTypefloat64(default) orfloat32.outNDArrayOptional output array.
Returns
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
sizeShapeOutput shape. If default/scalar a single value is returned.
dtypeDTypeDesired dtype —
float64(default) orfloat32.outNDArrayOptional output array.
Returns
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
lowdoubleLower boundary (inclusive). Default 0.
highdoubleUpper boundary (exclusive). Default 1.
sizeShapeOutput shape.
Returns
Remarks
https://numpy.org/doc/stable/reference/random/generated/numpy.random.Generator.uniform.html
low + (high - low) * next_double(), byte-identical to NumPy.