pub struct Generator<B: BitGenerator = Xoshiro256StarStar> { /* private fields */ }Expand description
The main random number generator, wrapping a pluggable BitGenerator.
Generator takes &mut self for all sampling methods — it is stateful
and NOT Sync. Thread-safety is handled by spawning independent generators
via spawn or using the parallel generation API.
§Example
use ferray_random::{default_rng_seeded, Generator};
let mut rng = default_rng_seeded(42);
let values = rng.random(10).unwrap();
assert_eq!(values.shape(), &[10]);Implementations§
Source§impl<B: BitGenerator> Generator<B>
impl<B: BitGenerator> Generator<B>
Sourcepub fn binomial(
&mut self,
n: u64,
p: f64,
size: impl IntoShape,
) -> Result<Array<i64, IxDyn>, FerrayError>
pub fn binomial( &mut self, n: u64, p: f64, size: impl IntoShape, ) -> Result<Array<i64, IxDyn>, FerrayError>
Generate an array of binomial-distributed variates.
Each value is the number of successes in n Bernoulli trials
with success probability p.
§Arguments
n- Number of trials.p- Probability of success per trial, must be in [0, 1].size- Number of values to generate.
§Errors
Returns FerrayError::InvalidValue for invalid parameters.
Sourcepub fn negative_binomial(
&mut self,
n: f64,
p: f64,
size: impl IntoShape,
) -> Result<Array<i64, IxDyn>, FerrayError>
pub fn negative_binomial( &mut self, n: f64, p: f64, size: impl IntoShape, ) -> Result<Array<i64, IxDyn>, FerrayError>
Generate an array of negative binomial distributed variates.
The number of failures before n successes with success probability p.
Uses the gamma-Poisson mixture.
§Arguments
n- Number of successes (positive).p- Probability of success, must be in (0, 1].size- Number of values to generate.
§Errors
Returns FerrayError::InvalidValue for invalid parameters.
Sourcepub fn poisson(
&mut self,
lam: f64,
size: impl IntoShape,
) -> Result<Array<i64, IxDyn>, FerrayError>
pub fn poisson( &mut self, lam: f64, size: impl IntoShape, ) -> Result<Array<i64, IxDyn>, FerrayError>
Sourcepub fn poisson_array(
&mut self,
lam: &Array<f64, IxDyn>,
) -> Result<Array<i64, IxDyn>, FerrayError>
pub fn poisson_array( &mut self, lam: &Array<f64, IxDyn>, ) -> Result<Array<i64, IxDyn>, FerrayError>
Generate an array of Poisson variates with a broadcast lam
parameter (#449).
lam is an array of expected counts; each output element is
sampled from Poisson(lam[i]). Output shape equals
lam.shape().
§Errors
FerrayError::InvalidValueif anylamelement is negative.
Sourcepub fn geometric(
&mut self,
p: f64,
size: impl IntoShape,
) -> Result<Array<i64, IxDyn>, FerrayError>
pub fn geometric( &mut self, p: f64, size: impl IntoShape, ) -> Result<Array<i64, IxDyn>, FerrayError>
Sourcepub fn hypergeometric(
&mut self,
ngood: u64,
nbad: u64,
nsample: u64,
size: impl IntoShape,
) -> Result<Array<i64, IxDyn>, FerrayError>
pub fn hypergeometric( &mut self, ngood: u64, nbad: u64, nsample: u64, size: impl IntoShape, ) -> Result<Array<i64, IxDyn>, FerrayError>
Generate an array of hypergeometric-distributed variates.
Models drawing nsample items without replacement from a population
containing ngood success states and nbad failure states.
§Arguments
ngood- Number of success states in the population.nbad- Number of failure states in the population.nsample- Number of items drawn.size- Number of values to generate.
§Errors
Returns FerrayError::InvalidValue if nsample > ngood + nbad or size is zero.
Sourcepub fn logseries(
&mut self,
p: f64,
size: impl IntoShape,
) -> Result<Array<i64, IxDyn>, FerrayError>
pub fn logseries( &mut self, p: f64, size: impl IntoShape, ) -> Result<Array<i64, IxDyn>, FerrayError>
Sourcepub fn zipf(
&mut self,
a: f64,
size: impl IntoShape,
) -> Result<Array<i64, IxDyn>, FerrayError>
pub fn zipf( &mut self, a: f64, size: impl IntoShape, ) -> Result<Array<i64, IxDyn>, FerrayError>
Generate an array of Zipf-distributed variates.
Samples from the Zipf (zeta) distribution with shape parameter a > 1,
using Devroye’s rejection algorithm (Non-Uniform Random Variate
Generation, p. 551). The PMF is P(k) = k^(-a) / zeta(a) for
k = 1, 2, ....
Equivalent to numpy.random.Generator.zipf.
§Errors
FerrayError::InvalidValueifa <= 1orsizeis invalid.
Source§impl<B: BitGenerator> Generator<B>
impl<B: BitGenerator> Generator<B>
Sourcepub fn standard_exponential(
&mut self,
size: impl IntoShape,
) -> Result<Array<f64, IxDyn>, FerrayError>
pub fn standard_exponential( &mut self, size: impl IntoShape, ) -> Result<Array<f64, IxDyn>, FerrayError>
Generate an array of standard exponential (rate=1, scale=1) variates.
Uses the inverse CDF method: -ln(U) where U ~ Uniform(0,1).
§Errors
Returns FerrayError::InvalidValue if shape is invalid.
Sourcepub fn exponential(
&mut self,
scale: f64,
size: impl IntoShape,
) -> Result<Array<f64, IxDyn>, FerrayError>
pub fn exponential( &mut self, scale: f64, size: impl IntoShape, ) -> Result<Array<f64, IxDyn>, FerrayError>
Generate an array of exponential variates with the given scale.
The exponential distribution has PDF: f(x) = (1/scale) * exp(-x/scale).
§Errors
Returns FerrayError::InvalidValue if scale <= 0 or shape is invalid.
Sourcepub fn standard_exponential_into(
&mut self,
out: &mut Array<f64, IxDyn>,
) -> Result<(), FerrayError>
pub fn standard_exponential_into( &mut self, out: &mut Array<f64, IxDyn>, ) -> Result<(), FerrayError>
Fill a pre-allocated out buffer with standard exponential
(rate=1) variates (#454).
Equivalent to numpy.random.Generator.standard_exponential(out=buffer).
§Errors
FerrayError::InvalidValue if out is non-contiguous.
Sourcepub fn exponential_array(
&mut self,
scale: &Array<f64, IxDyn>,
) -> Result<Array<f64, IxDyn>, FerrayError>
pub fn exponential_array( &mut self, scale: &Array<f64, IxDyn>, ) -> Result<Array<f64, IxDyn>, FerrayError>
Generate an array of exponential variates with a broadcast scale parameter (#449).
scale is an array; each output element is sampled from
Exp(rate = 1/scale[i]). The output shape equals
scale.shape().
§Errors
FerrayError::InvalidValueif anyscaleelement is<= 0.
Sourcepub fn standard_exponential_f32(
&mut self,
size: impl IntoShape,
) -> Result<Array<f32, IxDyn>, FerrayError>
pub fn standard_exponential_f32( &mut self, size: impl IntoShape, ) -> Result<Array<f32, IxDyn>, FerrayError>
Generate an array of standard exponential f32 variates.
The f32 analogue of standard_exponential.
§Errors
Returns FerrayError::InvalidValue if shape is invalid.
Sourcepub fn exponential_f32(
&mut self,
scale: f32,
size: impl IntoShape,
) -> Result<Array<f32, IxDyn>, FerrayError>
pub fn exponential_f32( &mut self, scale: f32, size: impl IntoShape, ) -> Result<Array<f32, IxDyn>, FerrayError>
Generate an array of f32 exponential variates with the given scale.
The f32 analogue of exponential.
§Errors
Returns FerrayError::InvalidValue if scale <= 0 or shape is invalid.
Source§impl<B: BitGenerator> Generator<B>
impl<B: BitGenerator> Generator<B>
Sourcepub fn standard_gamma(
&mut self,
alpha: f64,
size: impl IntoShape,
) -> Result<Array<f64, IxDyn>, FerrayError>
pub fn standard_gamma( &mut self, alpha: f64, size: impl IntoShape, ) -> Result<Array<f64, IxDyn>, FerrayError>
Generate an array of standard gamma variates with shape parameter alpha.
§Errors
Returns FerrayError::InvalidValue if alpha <= 0 or size is invalid.
Sourcepub fn gamma(
&mut self,
alpha: f64,
scale: f64,
size: impl IntoShape,
) -> Result<Array<f64, IxDyn>, FerrayError>
pub fn gamma( &mut self, alpha: f64, scale: f64, size: impl IntoShape, ) -> Result<Array<f64, IxDyn>, FerrayError>
Generate an array of gamma-distributed variates.
The gamma distribution with shape alpha and scale scale has
PDF: f(x) = x^(alpha-1) * exp(-x/scale) / (scale^alpha * Gamma(alpha)).
§Errors
Returns FerrayError::InvalidValue if alpha <= 0, scale <= 0, or size is invalid.
Sourcepub fn beta(
&mut self,
a: f64,
b: f64,
size: impl IntoShape,
) -> Result<Array<f64, IxDyn>, FerrayError>
pub fn beta( &mut self, a: f64, b: f64, size: impl IntoShape, ) -> Result<Array<f64, IxDyn>, FerrayError>
Generate an array of beta-distributed variates in (0, 1).
Uses the relationship: if X ~ Gamma(a), Y ~ Gamma(b), then X/(X+Y) ~ Beta(a,b).
§Errors
Returns FerrayError::InvalidValue if a <= 0, b <= 0, or size is invalid.
Sourcepub fn chisquare(
&mut self,
df: f64,
size: impl IntoShape,
) -> Result<Array<f64, IxDyn>, FerrayError>
pub fn chisquare( &mut self, df: f64, size: impl IntoShape, ) -> Result<Array<f64, IxDyn>, FerrayError>
Generate an array of chi-squared distributed variates.
Chi-squared(df) = Gamma(df/2, 2).
§Errors
Returns FerrayError::InvalidValue if df <= 0 or size is invalid.
Sourcepub fn f(
&mut self,
dfnum: f64,
dfden: f64,
size: impl IntoShape,
) -> Result<Array<f64, IxDyn>, FerrayError>
pub fn f( &mut self, dfnum: f64, dfden: f64, size: impl IntoShape, ) -> Result<Array<f64, IxDyn>, FerrayError>
Generate an array of F-distributed variates.
F(d1, d2) = (Chi2(d1)/d1) / (Chi2(d2)/d2).
§Errors
Returns FerrayError::InvalidValue if either df is non-positive or size is invalid.
Sourcepub fn student_t(
&mut self,
df: f64,
size: impl IntoShape,
) -> Result<Array<f64, IxDyn>, FerrayError>
pub fn student_t( &mut self, df: f64, size: impl IntoShape, ) -> Result<Array<f64, IxDyn>, FerrayError>
Generate an array of Student’s t-distributed variates.
t(df) = Normal(0,1) / sqrt(Chi2(df)/df).
§Errors
Returns FerrayError::InvalidValue if df <= 0 or size is invalid.
Sourcepub fn standard_t(
&mut self,
df: f64,
size: impl IntoShape,
) -> Result<Array<f64, IxDyn>, FerrayError>
pub fn standard_t( &mut self, df: f64, size: impl IntoShape, ) -> Result<Array<f64, IxDyn>, FerrayError>
Sourcepub fn noncentral_chisquare(
&mut self,
df: f64,
nonc: f64,
size: impl IntoShape,
) -> Result<Array<f64, IxDyn>, FerrayError>
pub fn noncentral_chisquare( &mut self, df: f64, nonc: f64, size: impl IntoShape, ) -> Result<Array<f64, IxDyn>, FerrayError>
Generate an array of noncentral chi-squared variates.
noncentral_chisquare(df, nonc) is the distribution of
sum((Z_i + mu_i)^2) where Z_i ~ N(0,1) and the sum of the
mu_i^2 equals nonc. Implemented via the Poisson-mixed central
chi-squared method: N ~ Poisson(nonc/2), X = 2 * Gamma((df + 2N)/2).
Equivalent to numpy.random.Generator.noncentral_chisquare.
§Errors
FerrayError::InvalidValueifdf <= 0,nonc < 0, orsizeis invalid.
Sourcepub fn noncentral_f(
&mut self,
dfnum: f64,
dfden: f64,
nonc: f64,
size: impl IntoShape,
) -> Result<Array<f64, IxDyn>, FerrayError>
pub fn noncentral_f( &mut self, dfnum: f64, dfden: f64, nonc: f64, size: impl IntoShape, ) -> Result<Array<f64, IxDyn>, FerrayError>
Generate an array of noncentral F-distributed variates.
noncentral_f(dfnum, dfden, nonc) = (Chi2_nc(dfnum, nonc)/dfnum) / (Chi2(dfden)/dfden).
Equivalent to numpy.random.Generator.noncentral_f.
§Errors
FerrayError::InvalidValueif any df is non-positive,nonc < 0, orsizeis invalid.
Source§impl<B: BitGenerator> Generator<B>
impl<B: BitGenerator> Generator<B>
Sourcepub fn laplace(
&mut self,
loc: f64,
scale: f64,
size: impl IntoShape,
) -> Result<Array<f64, IxDyn>, FerrayError>
pub fn laplace( &mut self, loc: f64, scale: f64, size: impl IntoShape, ) -> Result<Array<f64, IxDyn>, FerrayError>
Sourcepub fn logistic(
&mut self,
loc: f64,
scale: f64,
size: impl IntoShape,
) -> Result<Array<f64, IxDyn>, FerrayError>
pub fn logistic( &mut self, loc: f64, scale: f64, size: impl IntoShape, ) -> Result<Array<f64, IxDyn>, FerrayError>
Sourcepub fn rayleigh(
&mut self,
scale: f64,
size: impl IntoShape,
) -> Result<Array<f64, IxDyn>, FerrayError>
pub fn rayleigh( &mut self, scale: f64, size: impl IntoShape, ) -> Result<Array<f64, IxDyn>, FerrayError>
Sourcepub fn weibull(
&mut self,
a: f64,
size: impl IntoShape,
) -> Result<Array<f64, IxDyn>, FerrayError>
pub fn weibull( &mut self, a: f64, size: impl IntoShape, ) -> Result<Array<f64, IxDyn>, FerrayError>
Sourcepub fn pareto(
&mut self,
a: f64,
size: impl IntoShape,
) -> Result<Array<f64, IxDyn>, FerrayError>
pub fn pareto( &mut self, a: f64, size: impl IntoShape, ) -> Result<Array<f64, IxDyn>, FerrayError>
Generate an array of Pareto (type II / Lomax) distributed variates.
Uses inverse CDF: (1-u)^(-1/a) - 1 (then shifted by 1 to match NumPy).
NumPy’s Pareto: samples from Pareto(a) with x_m=1, so PDF = a / x^(a+1) for x >= 1.
§Arguments
a- Shape parameter, must be positive.size- Number of values to generate.
§Errors
Returns FerrayError::InvalidValue if a <= 0 or size is zero.
Sourcepub fn gumbel(
&mut self,
loc: f64,
scale: f64,
size: impl IntoShape,
) -> Result<Array<f64, IxDyn>, FerrayError>
pub fn gumbel( &mut self, loc: f64, scale: f64, size: impl IntoShape, ) -> Result<Array<f64, IxDyn>, FerrayError>
Sourcepub fn power(
&mut self,
a: f64,
size: impl IntoShape,
) -> Result<Array<f64, IxDyn>, FerrayError>
pub fn power( &mut self, a: f64, size: impl IntoShape, ) -> Result<Array<f64, IxDyn>, FerrayError>
Sourcepub fn triangular(
&mut self,
left: f64,
mode: f64,
right: f64,
size: impl IntoShape,
) -> Result<Array<f64, IxDyn>, FerrayError>
pub fn triangular( &mut self, left: f64, mode: f64, right: f64, size: impl IntoShape, ) -> Result<Array<f64, IxDyn>, FerrayError>
Sourcepub fn vonmises(
&mut self,
mu: f64,
kappa: f64,
size: impl IntoShape,
) -> Result<Array<f64, IxDyn>, FerrayError>
pub fn vonmises( &mut self, mu: f64, kappa: f64, size: impl IntoShape, ) -> Result<Array<f64, IxDyn>, FerrayError>
Generate an array of von Mises distributed variates.
The von Mises distribution is a continuous distribution on the circle. Uses Best & Fisher’s algorithm.
§Arguments
mu- Mean direction (in radians).kappa- Concentration parameter, must be non-negative.size- Number of values to generate.
§Errors
Returns FerrayError::InvalidValue if kappa < 0 or size is zero.
Sourcepub fn wald(
&mut self,
mean: f64,
scale: f64,
size: impl IntoShape,
) -> Result<Array<f64, IxDyn>, FerrayError>
pub fn wald( &mut self, mean: f64, scale: f64, size: impl IntoShape, ) -> Result<Array<f64, IxDyn>, FerrayError>
Generate an array of Wald (inverse Gaussian) distributed variates.
§Arguments
mean- Mean of the distribution, must be positive.scale- Scale parameter (lambda), must be positive.size- Number of values to generate.
§Errors
Returns FerrayError::InvalidValue if mean <= 0, scale <= 0, or size is zero.
Sourcepub fn standard_cauchy(
&mut self,
size: impl IntoShape,
) -> Result<Array<f64, IxDyn>, FerrayError>
pub fn standard_cauchy( &mut self, size: impl IntoShape, ) -> Result<Array<f64, IxDyn>, FerrayError>
Source§impl<B: BitGenerator> Generator<B>
impl<B: BitGenerator> Generator<B>
Sourcepub fn multinomial(
&mut self,
n: u64,
pvals: &[f64],
size: usize,
) -> Result<Array<i64, Ix2>, FerrayError>
pub fn multinomial( &mut self, n: u64, pvals: &[f64], size: usize, ) -> Result<Array<i64, Ix2>, FerrayError>
Generate multinomial samples.
Each row of the output is one draw of n items distributed across
k categories with probabilities pvals.
§Arguments
n- Number of trials per sample.pvals- Category probabilities (must sum to ~1.0, length k).size- Number of multinomial draws (rows in output).
§Returns
An Array<i64, Ix2> with shape [size, k].
§Errors
Returns FerrayError::InvalidValue for invalid parameters.
Sourcepub fn multivariate_normal(
&mut self,
mean: &[f64],
cov: &[f64],
size: usize,
) -> Result<Array<f64, Ix2>, FerrayError>
pub fn multivariate_normal( &mut self, mean: &[f64], cov: &[f64], size: usize, ) -> Result<Array<f64, Ix2>, FerrayError>
Generate multivariate normal samples.
Uses the Cholesky decomposition of the covariance matrix.
§Arguments
mean- Mean vector of lengthd.cov- Covariance matrix, flattened in row-major order, shape[d, d].size- Number of samples (rows in output).
§Returns
An Array<f64, Ix2> with shape [size, d].
§Errors
Returns FerrayError::InvalidValue for invalid parameters or if
the covariance matrix is not positive semi-definite.
Sourcepub fn multivariate_hypergeometric(
&mut self,
colors: &[u64],
nsample: u64,
size: usize,
) -> Result<Array<i64, Ix2>, FerrayError>
pub fn multivariate_hypergeometric( &mut self, colors: &[u64], nsample: u64, size: usize, ) -> Result<Array<i64, Ix2>, FerrayError>
Generate samples from the multivariate hypergeometric distribution.
Models drawing nsample items without replacement from a population
partitioned into K colors with colors[k] items of color k.
Each row of the output is one such draw — a vector of K non-negative
counts summing to nsample.
Uses the marginals algorithm: a sequence of K-1 univariate
hypergeometric draws, each picking the count of one color from the
remainder of the population. The final color is what’s left. This
matches numpy.random.Generator.multivariate_hypergeometric (#445).
§Arguments
colors- Number of items of each color (length K, all non-negative).nsample- Number of items drawn per sample (must be ≤ sum ofcolors).size- Number of multivariate draws (rows in output).
§Returns
An Array<i64, Ix2> with shape [size, K].
§Errors
Returns FerrayError::InvalidValue if colors is empty, size is 0,
or nsample exceeds the population total.
Sourcepub fn multivariate_normal_array(
&mut self,
mean: &Array<f64, Ix1>,
cov: &Array<f64, Ix2>,
size: usize,
) -> Result<Array<f64, Ix2>, FerrayError>
pub fn multivariate_normal_array( &mut self, mean: &Array<f64, Ix1>, cov: &Array<f64, Ix2>, size: usize, ) -> Result<Array<f64, Ix2>, FerrayError>
Generate multivariate normal samples taking Array parameters.
Ergonomic counterpart to [multivariate_normal] (#451): accepts
the mean as Array<f64, Ix1> and the covariance as
Array<f64, Ix2> directly, no manual flattening required.
Cholesky decomposition is delegated to ferray_linalg::cholesky
(#452) which is faer-backed and surfaces non-positive-definite
inputs as FerrayError::SingularMatrix instead of the
home-grown cholesky_decompose helper.
§Errors
FerrayError::ShapeMismatchifcovis not square or its side does not matchmean.len().FerrayError::SingularMatrixifcovis not positive definite (propagated fromferray-linalg).FerrayError::InvalidValuefor size = 0 or empty mean.
Sourcepub fn dirichlet(
&mut self,
alpha: &[f64],
size: usize,
) -> Result<Array<f64, Ix2>, FerrayError>
pub fn dirichlet( &mut self, alpha: &[f64], size: usize, ) -> Result<Array<f64, Ix2>, FerrayError>
Generate Dirichlet-distributed samples.
Each row is a sample from the Dirichlet distribution parameterized
by alpha, producing vectors that sum to 1.
§Arguments
alpha- Concentration parameters (all must be positive).size- Number of samples (rows in output).
§Returns
An Array<f64, Ix2> with shape [size, k] where k = alpha.len().
§Errors
Returns FerrayError::InvalidValue for invalid parameters.
Source§impl<B: BitGenerator> Generator<B>
impl<B: BitGenerator> Generator<B>
Sourcepub fn standard_normal(
&mut self,
size: impl IntoShape,
) -> Result<Array<f64, IxDyn>, FerrayError>
pub fn standard_normal( &mut self, size: impl IntoShape, ) -> Result<Array<f64, IxDyn>, FerrayError>
Generate an array of standard normal (mean=0, std=1) variates.
Uses the Marsaglia–Tsang Ziggurat algorithm (256 layers), which is
roughly 3× faster than Box–Muller for large draws. shape accepts
usize, [usize; N], &[usize], or Vec<usize> via IntoShape.
§Errors
Returns FerrayError::InvalidValue if shape is invalid.
Sourcepub fn normal(
&mut self,
loc: f64,
scale: f64,
size: impl IntoShape,
) -> Result<Array<f64, IxDyn>, FerrayError>
pub fn normal( &mut self, loc: f64, scale: f64, size: impl IntoShape, ) -> Result<Array<f64, IxDyn>, FerrayError>
Generate an array of normal (Gaussian) variates with given mean and
standard deviation. Equivalent to numpy.random.Generator.normal.
§Errors
Returns FerrayError::InvalidValue if scale <= 0 or shape is invalid.
Sourcepub fn standard_normal_into(
&mut self,
out: &mut Array<f64, IxDyn>,
) -> Result<(), FerrayError>
pub fn standard_normal_into( &mut self, out: &mut Array<f64, IxDyn>, ) -> Result<(), FerrayError>
Fill a pre-allocated out buffer with standard normal
(mean=0, std=1) variates (#454).
Equivalent to numpy.random.Generator.standard_normal(out=buffer).
Each slot is overwritten with a fresh Ziggurat draw — no heap
allocation.
§Errors
FerrayError::InvalidValue if out is non-contiguous.
Sourcepub fn normal_array(
&mut self,
loc: &Array<f64, IxDyn>,
scale: &Array<f64, IxDyn>,
) -> Result<Array<f64, IxDyn>, FerrayError>
pub fn normal_array( &mut self, loc: &Array<f64, IxDyn>, scale: &Array<f64, IxDyn>, ) -> Result<Array<f64, IxDyn>, FerrayError>
Generate an array of normal variates with broadcast array parameters (#449).
loc and scale are arrays that NumPy-broadcast against each
other to produce the output shape. Each output element is
loc[i] + scale[i] * Z where Z is a fresh standard-normal
draw. Equivalent to numpy.random.Generator.normal(loc, scale)
when both are arrays.
For scalar parameters, prefer normal — it
avoids the broadcast view and is faster.
§Errors
FerrayError::ShapeMismatchif the two shapes are not broadcast-compatible.FerrayError::InvalidValueif anyscaleelement is<= 0.
Sourcepub fn standard_normal_f32(
&mut self,
size: impl IntoShape,
) -> Result<Array<f32, IxDyn>, FerrayError>
pub fn standard_normal_f32( &mut self, size: impl IntoShape, ) -> Result<Array<f32, IxDyn>, FerrayError>
Generate an array of standard normal (mean=0, std=1) f32 variates.
The f32 analogue of standard_normal. Equivalent
to NumPy’s Generator.standard_normal(size, dtype=np.float32). Uses the
same Ziggurat f64 tables, then casts to f32 to preserve tail accuracy.
§Errors
Returns FerrayError::InvalidValue if shape is invalid.
Sourcepub fn normal_f32(
&mut self,
loc: f32,
scale: f32,
size: impl IntoShape,
) -> Result<Array<f32, IxDyn>, FerrayError>
pub fn normal_f32( &mut self, loc: f32, scale: f32, size: impl IntoShape, ) -> Result<Array<f32, IxDyn>, FerrayError>
Sourcepub fn lognormal_f32(
&mut self,
mean: f32,
sigma: f32,
size: impl IntoShape,
) -> Result<Array<f32, IxDyn>, FerrayError>
pub fn lognormal_f32( &mut self, mean: f32, sigma: f32, size: impl IntoShape, ) -> Result<Array<f32, IxDyn>, FerrayError>
Sourcepub fn lognormal(
&mut self,
mean: f64,
sigma: f64,
size: impl IntoShape,
) -> Result<Array<f64, IxDyn>, FerrayError>
pub fn lognormal( &mut self, mean: f64, sigma: f64, size: impl IntoShape, ) -> Result<Array<f64, IxDyn>, FerrayError>
Generate an array of log-normal variates.
If X ~ Normal(mean, sigma), then exp(X) ~ LogNormal(mean, sigma).
§Errors
Returns FerrayError::InvalidValue if sigma <= 0 or shape is invalid.
Source§impl<B: BitGenerator> Generator<B>
impl<B: BitGenerator> Generator<B>
Sourcepub fn random(
&mut self,
size: impl IntoShape,
) -> Result<Array<f64, IxDyn>, FerrayError>
pub fn random( &mut self, size: impl IntoShape, ) -> Result<Array<f64, IxDyn>, FerrayError>
Generate an array of uniformly distributed f64 values in [0, 1).
Equivalent to NumPy’s Generator.random(size). shape may be a
usize for a 1-D result, or any [usize; N] / &[usize] / Vec<usize>
for N-dimensional output (via IntoShape).
§Errors
Returns FerrayError::InvalidValue if shape contains a zero-sized axis.
§Example
let mut rng = ferray_random::default_rng_seeded(42);
let v = rng.random(10).unwrap();
assert_eq!(v.shape(), &[10]);
let m = rng.random([3, 4]).unwrap();
assert_eq!(m.shape(), &[3, 4]);Sourcepub fn random_into(
&mut self,
out: &mut Array<f64, IxDyn>,
) -> Result<(), FerrayError>
pub fn random_into( &mut self, out: &mut Array<f64, IxDyn>, ) -> Result<(), FerrayError>
Fill a pre-allocated out buffer with uniform [0, 1) variates (#454).
Equivalent to numpy.random.Generator.random(out=buffer) — the
allocation is the caller’s, so hot loops that produce many
equal-shaped batches reuse a single buffer instead of churning
the heap.
§Errors
FerrayError::InvalidValue if out is non-contiguous.
Sourcepub fn uniform(
&mut self,
low: f64,
high: f64,
size: impl IntoShape,
) -> Result<Array<f64, IxDyn>, FerrayError>
pub fn uniform( &mut self, low: f64, high: f64, size: impl IntoShape, ) -> Result<Array<f64, IxDyn>, FerrayError>
Generate an array of uniformly distributed f64 values in [low, high).
Equivalent to NumPy’s Generator.uniform(low, high, size).
§Errors
Returns FerrayError::InvalidValue if low >= high or shape is invalid.
Sourcepub fn uniform_array(
&mut self,
low: &Array<f64, IxDyn>,
high: &Array<f64, IxDyn>,
) -> Result<Array<f64, IxDyn>, FerrayError>
pub fn uniform_array( &mut self, low: &Array<f64, IxDyn>, high: &Array<f64, IxDyn>, ) -> Result<Array<f64, IxDyn>, FerrayError>
Generate an array of uniform variates with broadcast array parameters (#449).
low and high are arrays that NumPy-broadcast to determine
the output shape. Each element is drawn from
Uniform([low[i], high[i])). Equivalent to
numpy.random.Generator.uniform(low, high) when both are arrays.
§Errors
FerrayError::ShapeMismatchif shapes don’t broadcast.FerrayError::InvalidValueif any element haslow >= high.
Sourcepub fn random_f32(
&mut self,
size: impl IntoShape,
) -> Result<Array<f32, IxDyn>, FerrayError>
pub fn random_f32( &mut self, size: impl IntoShape, ) -> Result<Array<f32, IxDyn>, FerrayError>
Generate an array of uniformly distributed f32 values in [0, 1).
The f32 analogue of random. Equivalent to
NumPy’s Generator.random(size, dtype=np.float32).
§Errors
Returns FerrayError::InvalidValue if shape contains a zero-sized axis.
§Example
let mut rng = ferray_random::default_rng_seeded(42);
let v = rng.random_f32(10).unwrap();
assert_eq!(v.shape(), &[10]);
for &x in v.iter() {
assert!((0.0..1.0).contains(&x));
}Sourcepub fn uniform_f32(
&mut self,
low: f32,
high: f32,
size: impl IntoShape,
) -> Result<Array<f32, IxDyn>, FerrayError>
pub fn uniform_f32( &mut self, low: f32, high: f32, size: impl IntoShape, ) -> Result<Array<f32, IxDyn>, FerrayError>
Sourcepub fn integers(
&mut self,
low: i64,
high: i64,
size: impl IntoShape,
) -> Result<Array<i64, IxDyn>, FerrayError>
pub fn integers( &mut self, low: i64, high: i64, size: impl IntoShape, ) -> Result<Array<i64, IxDyn>, FerrayError>
Generate an array of uniformly distributed random integers in [low, high).
Equivalent to NumPy’s Generator.integers(low, high, size).
§Errors
Returns FerrayError::InvalidValue if low >= high or shape is invalid.
Source§impl<B: BitGenerator> Generator<B>
impl<B: BitGenerator> Generator<B>
Source§impl<B: BitGenerator> Generator<B>
impl<B: BitGenerator> Generator<B>
Source§impl<B: BitGenerator> Generator<B>
impl<B: BitGenerator> Generator<B>
Source§impl<B: BitGenerator> Generator<B>
impl<B: BitGenerator> Generator<B>
Source§impl<B: BitGenerator> Generator<B>
impl<B: BitGenerator> Generator<B>
Source§impl<B: BitGenerator> Generator<B>
impl<B: BitGenerator> Generator<B>
Source§impl<B: BitGenerator> Generator<B>
impl<B: BitGenerator> Generator<B>
Source§impl<B: BitGenerator> Generator<B>
impl<B: BitGenerator> Generator<B>
Sourcepub const fn bit_generator(&mut self) -> &mut B
pub const fn bit_generator(&mut self) -> &mut B
Access the underlying BitGenerator mutably.
Sourcepub fn next_u64_bounded(&mut self, bound: u64) -> u64
pub fn next_u64_bounded(&mut self, bound: u64) -> u64
Generate a u64 in [0, bound).
Sourcepub fn state_bytes(&self) -> Result<Vec<u8>, FerrayError>
pub fn state_bytes(&self) -> Result<Vec<u8>, FerrayError>
Serialize the underlying BitGenerator’s full internal state
to a byte vector — pair with set_state_bytes
to restore. Used to checkpoint reproducible experiments (#453).
The format is the LE-byte serialization of the bit generator’s
state words; it is stable per-generator-type but not
portable across different BitGenerator implementations
(Pcg64 state cannot be loaded into Xoshiro256**).
§Errors
FerrayError::InvalidValue if the underlying generator does
not implement state serialization.
Sourcepub fn set_state_bytes(&mut self, bytes: &[u8]) -> Result<(), FerrayError>
pub fn set_state_bytes(&mut self, bytes: &[u8]) -> Result<(), FerrayError>
Restore the underlying BitGenerator’s state from previously
captured bytes.
§Errors
FerrayError::InvalidValue if the byte length is wrong for
this generator type or the embedded state is invalid (e.g.
all-zero state for Xoshiro256**, even inc for Pcg64).
Sourcepub fn bytes(&mut self, n: usize) -> Vec<u8> ⓘ
pub fn bytes(&mut self, n: usize) -> Vec<u8> ⓘ
Generate n random bytes as a Vec<u8>.
Equivalent to numpy.random.Generator.bytes(n). Each byte is
drawn from the underlying bit generator’s u64 stream and
little-endian-decomposed; calling bytes(n) advances the bit
generator by ceil(n / 8) u64 draws (#446).
Source§impl<B: BitGenerator + Clone> Generator<B>
impl<B: BitGenerator + Clone> Generator<B>
Sourcepub fn standard_normal_parallel(
&mut self,
size: impl IntoShape,
) -> Result<Array<f64, IxDyn>, FerrayError>
pub fn standard_normal_parallel( &mut self, size: impl IntoShape, ) -> Result<Array<f64, IxDyn>, FerrayError>
Generate standard normal variates in parallel, deterministically.
Uses spawn() to create independent child generators (via jump-ahead
for Xoshiro256**, stream IDs for Philox, or seed-from-parent for
PCG64), then generates chunks in parallel using Rayon.
The output is deterministic for a given seed and thread count,
but does not match standard_normal(size) (which is sequential).
The parallel version produces different values because the child
generators have different internal states.
§Errors
Returns FerrayError::InvalidValue if shape is invalid.
Source§impl<B: BitGenerator> Generator<B>
impl<B: BitGenerator> Generator<B>
Sourcepub fn shuffle<T>(&mut self, arr: &mut Array<T, Ix1>) -> Result<(), FerrayError>where
T: Element,
pub fn shuffle<T>(&mut self, arr: &mut Array<T, Ix1>) -> Result<(), FerrayError>where
T: Element,
Shuffle a 1-D array in-place using Fisher-Yates.
§Errors
Returns FerrayError::InvalidValue if the array is not contiguous.
Sourcepub fn permutation<T>(
&mut self,
arr: &Array<T, Ix1>,
) -> Result<Array<T, Ix1>, FerrayError>where
T: Element,
pub fn permutation<T>(
&mut self,
arr: &Array<T, Ix1>,
) -> Result<Array<T, Ix1>, FerrayError>where
T: Element,
Return a new array with elements randomly permuted.
If the input is 1-D, returns a shuffled copy. If an integer n is
given (via permutation_range), returns a permutation of 0..n.
§Errors
Returns FerrayError::InvalidValue if the array is empty.
Sourcepub fn permutation_range(
&mut self,
n: usize,
) -> Result<Array<i64, Ix1>, FerrayError>
pub fn permutation_range( &mut self, n: usize, ) -> Result<Array<i64, Ix1>, FerrayError>
Return a permutation of 0..n as an Array1<i64>.
§Errors
Returns FerrayError::InvalidValue if n is zero.
Sourcepub fn permuted<T>(
&mut self,
arr: &Array<T, Ix1>,
_axis: usize,
) -> Result<Array<T, Ix1>, FerrayError>where
T: Element,
pub fn permuted<T>(
&mut self,
arr: &Array<T, Ix1>,
_axis: usize,
) -> Result<Array<T, Ix1>, FerrayError>where
T: Element,
Return an array with elements independently permuted along the given axis.
For 1-D arrays, this is the same as permutation. This simplified
implementation operates on 1-D arrays along axis 0.
§Errors
Returns FerrayError::InvalidValue if the array is empty.
Sourcepub fn shuffle_dyn<T>(
&mut self,
arr: &mut Array<T, IxDyn>,
axis: usize,
) -> Result<(), FerrayError>where
T: Element,
pub fn shuffle_dyn<T>(
&mut self,
arr: &mut Array<T, IxDyn>,
axis: usize,
) -> Result<(), FerrayError>where
T: Element,
Shuffle an N-D array in place along axis, swapping whole
hyperslices (rows when axis == 0 for a 2-D array).
Equivalent to numpy.random.Generator.shuffle(x, axis=axis).
Each pair (i, j) selected by Fisher-Yates exchanges all
elements with axis-coordinate i and j simultaneously, so
rows / columns / N-D slices keep their internal structure (#447).
§Errors
FerrayError::AxisOutOfBoundsifaxis >= arr.ndim().FerrayError::InvalidValueifarris non-contiguous.
Sourcepub fn choice_dyn<T>(
&mut self,
arr: &Array<T, IxDyn>,
size: usize,
replace: bool,
p: Option<&[f64]>,
axis: usize,
shuffle: bool,
) -> Result<Array<T, IxDyn>, FerrayError>where
T: Element,
pub fn choice_dyn<T>(
&mut self,
arr: &Array<T, IxDyn>,
size: usize,
replace: bool,
p: Option<&[f64]>,
axis: usize,
shuffle: bool,
) -> Result<Array<T, IxDyn>, FerrayError>where
T: Element,
Sample N-D hyperslices from arr along axis (#448).
For each of size draws, picks an index along axis
(uniformly or weighted by p, with or without replacement)
and copies the corresponding (N-1)-D slice into the output.
The output has the same shape as arr with the axis-th
dimension replaced by size.
Equivalent to numpy.random.Generator.choice(arr, size, replace, p, axis)
for N-D arr. The shuffle parameter (numpy 1.24+) controls
whether the indices are returned in selection order
(shuffle = true, default) or sorted (shuffle = false,
only meaningful when replace = false).
§Errors
FerrayError::AxisOutOfBoundsifaxis >= arr.ndim().FerrayError::InvalidValueif the axis dimension is empty,size > axis_lenwithreplace = false,arris non-contiguous, orpis malformed.
Sourcepub fn permuted_dyn<T>(
&mut self,
arr: &Array<T, IxDyn>,
axis: usize,
) -> Result<Array<T, IxDyn>, FerrayError>where
T: Element,
pub fn permuted_dyn<T>(
&mut self,
arr: &Array<T, IxDyn>,
axis: usize,
) -> Result<Array<T, IxDyn>, FerrayError>where
T: Element,
Independently permute the entries along axis of arr.
Returns a new array. For each combination of “other” indices
(everything except axis) the values along axis are
shuffled with their own Fisher-Yates pass — so columns of a
2-D array get independent permutations when axis = 0.
Equivalent to numpy.random.Generator.permuted(x, axis=axis).
§Errors
FerrayError::AxisOutOfBoundsifaxis >= arr.ndim().FerrayError::InvalidValueifarris non-contiguous.
Sourcepub fn choice<T>(
&mut self,
arr: &Array<T, Ix1>,
size: usize,
replace: bool,
p: Option<&[f64]>,
) -> Result<Array<T, Ix1>, FerrayError>where
T: Element,
pub fn choice<T>(
&mut self,
arr: &Array<T, Ix1>,
size: usize,
replace: bool,
p: Option<&[f64]>,
) -> Result<Array<T, Ix1>, FerrayError>where
T: Element,
Randomly select elements from an array, with or without replacement.
§Arguments
arr- Source array to sample from.size- Number of elements to select.replace- Iftrue, sample with replacement; iffalse, without.p- Optional probability weights (must sum to 1.0 and have same length asarr).
§Errors
Returns FerrayError::InvalidValue if parameters are invalid (e.g.,
size > arr.len() when replace=false, or invalid probability weights).
Auto Trait Implementations§
impl<B> Freeze for Generator<B>where
B: Freeze,
impl<B> RefUnwindSafe for Generator<B>where
B: RefUnwindSafe,
impl<B> Send for Generator<B>
impl<B> Sync for Generator<B>where
B: Sync,
impl<B> Unpin for Generator<B>where
B: Unpin,
impl<B> UnsafeUnpin for Generator<B>where
B: UnsafeUnpin,
impl<B> UnwindSafe for Generator<B>where
B: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> DistributionExt for Twhere
T: ?Sized,
impl<T> DistributionExt for Twhere
T: ?Sized,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more