// Copyright 2019 Developers of the Rand project. // // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your // option. This file may not be copied, modified, or distributed // except according to those terms.
//! Generating random samples from probability distributions. //! //! ## Re-exports //! //! This crate is a super-set of the [`rand::distributions`] module. See the //! [`rand::distributions`] module documentation for an overview of the core //! [`Distribution`] trait and implementations. //! //! The following are re-exported: //! //! - The [`Distribution`] trait and [`DistIter`] helper type //! - The [`Standard`], [`Alphanumeric`], [`Uniform`], [`OpenClosed01`], //! [`Open01`], [`Bernoulli`], and [`WeightedIndex`] distributions //! //! ## Distributions //! //! This crate provides the following probability distributions: //! //! - Related to real-valued quantities that grow linearly //! (e.g. errors, offsets): //! - [`Normal`] distribution, and [`StandardNormal`] as a primitive //! - [`SkewNormal`] distribution //! - [`Cauchy`] distribution //! - Related to Bernoulli trials (yes/no events, with a given probability): //! - [`Binomial`] distribution //! - [`Geometric`] distribution //! - [`Hypergeometric`] distribution //! - Related to positive real-valued quantities that grow exponentially //! (e.g. prices, incomes, populations): //! - [`LogNormal`] distribution //! - Related to the occurrence of independent events at a given rate: //! - [`Pareto`] distribution //! - [`Poisson`] distribution //! - [`Exp`]onential distribution, and [`Exp1`] as a primitive //! - [`Weibull`] distribution //! - [`Gumbel`] distribution //! - [`Frechet`] distribution //! - [`Zeta`] distribution //! - [`Zipf`] distribution //! - Gamma and derived distributions: //! - [`Gamma`] distribution //! - [`ChiSquared`] distribution //! - [`StudentT`] distribution //! - [`FisherF`] distribution //! - Triangular distribution: //! - [`Beta`] distribution //! - [`Triangular`] distribution //! - Multivariate probability distributions //! - [`Dirichlet`] distribution //! - [`UnitSphere`] distribution //! - [`UnitBall`] distribution //! - [`UnitCircle`] distribution //! - [`UnitDisc`] distribution //! - Alternative implementation for weighted index sampling //! - [`WeightedAliasIndex`] distribution //! - Misc. distributions //! - [`InverseGaussian`] distribution //! - [`NormalInverseGaussian`] distribution
#[cfg(feature = "alloc")] externcrate alloc;
#[cfg(feature = "std")] externcrate std;
// This is used for doc links: #[allow(unused)] use rand::Rng;
pubuseself::binomial::{Binomial, Error as BinomialError}; pubuseself::cauchy::{Cauchy, Error as CauchyError}; #[cfg(feature = "alloc")] #[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))] pubuseself::dirichlet::{Dirichlet, Error as DirichletError}; pubuseself::exponential::{Error as ExpError, Exp, Exp1}; pubuseself::frechet::{Error as FrechetError, Frechet}; pubuseself::gamma::{
Beta, BetaError, ChiSquared, ChiSquaredError, Error as GammaError, FisherF, FisherFError,
Gamma, StudentT,
}; pubuseself::geometric::{Error as GeoError, Geometric, StandardGeometric}; pubuseself::gumbel::{Error as GumbelError, Gumbel}; pubuseself::hypergeometric::{Error as HyperGeoError, Hypergeometric}; pubuseself::inverse_gaussian::{Error as InverseGaussianError, InverseGaussian}; pubuseself::normal::{Error as NormalError, LogNormal, Normal, StandardNormal}; pubuseself::normal_inverse_gaussian::{
Error as NormalInverseGaussianError, NormalInverseGaussian,
}; pubuseself::pareto::{Error as ParetoError, Pareto}; pubuseself::pert::{Pert, PertError}; pubuseself::poisson::{Error as PoissonError, Poisson}; pubuseself::skew_normal::{Error as SkewNormalError, SkewNormal}; pubuseself::triangular::{Triangular, TriangularError}; pubuseself::unit_ball::UnitBall; pubuseself::unit_circle::UnitCircle; pubuseself::unit_disc::UnitDisc; pubuseself::unit_sphere::UnitSphere; pubuseself::weibull::{Error as WeibullError, Weibull}; pubuseself::zipf::{Zeta, ZetaError, Zipf, ZipfError}; #[cfg(feature = "alloc")] #[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))] pubuse rand::distributions::{WeightedError, WeightedIndex}; #[cfg(feature = "alloc")] #[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))] pubuse weighted_alias::WeightedAliasIndex;
pubuse num_traits;
#[cfg(test)] #[macro_use] mod test { // Notes on testing // // Testing random number distributions correctly is hard. The following // testing is desired: // // - Construction: test initialisation with a few valid parameter sets. // - Erroneous usage: test that incorrect usage generates an error. // - Vector: test that usage with fixed inputs (including RNG) generates a // fixed output sequence on all platforms. // - Correctness at fixed points (optional): using a specific mock RNG, // check that specific values are sampled (e.g. end-points and median of // distribution). // - Correctness of PDF (extra): generate a histogram of samples within a // certain range, and check this approximates the PDF. These tests are // expected to be expensive, and should be behind a feature-gate. // // TODO: Vector and correctness tests are largely absent so far. // NOTE: Some distributions have tests checking only that samples can be // generated. This is redundant with vector and correctness tests.
/// Construct a deterministic RNG with the given seed pubfn rng(seed: u64) -> impl rand::RngCore { // For tests, we want a statistically good, fast, reproducible RNG. // PCG32 will do fine, and will be easy to embed if we ever need to. const INC: u64 = 11634580027462260723;
rand_pcg::Pcg32::new(seed, INC)
}
/// Assert that two numbers are almost equal to each other. /// /// On panic, this macro will print the values of the expressions with their /// debug representations.
macro_rules! assert_almost_eq {
($a:expr, $b:expr, $prec:expr) => { let diff = ($a - $b).abs();
assert!(diff <= $prec, "assertion failed: `abs(left - right) = {:.1e} < {:e}`, \
(left: `{}`, right: `{}`)",
diff, $prec, $a, $b
);
};
}
}
mod binomial; mod cauchy; mod dirichlet; mod exponential; mod frechet; mod gamma; mod geometric; mod gumbel; mod hypergeometric; mod inverse_gaussian; mod normal; mod normal_inverse_gaussian; mod pareto; mod pert; mod poisson; mod skew_normal; mod triangular; mod unit_ball; mod unit_circle; mod unit_disc; mod unit_sphere; mod utils; mod weibull; mod ziggurat_tables; mod zipf;
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.13 Sekunden
(vorverarbeitet am 2026-06-18)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.