Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename StandardStandardUniform #1526

Merged
merged 7 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ You may also find the [Upgrade Guide](https://rust-random.github.io/book/update.
- Rename `rand::thread_rng()` to `rand::rng()`, and remove from the prelude (#1506)
- Remove `rand::random()` from the prelude (#1506)
- Rename `Rng::gen_range` to `random_range`, `gen_bool` to `random_bool`, `gen_ratio` to `random_ratio` (#1505)
- Rename `Standard` to `StandardUniform` (#1526)
- Remove impl of `Distribution<Option<T>>` for `Standard` (#1526)

dhardy marked this conversation as resolved.
Show resolved Hide resolved
## [0.9.0-alpha.1] - 2024-03-18
- Add the `Slice::num_choices` method to the Slice distribution (#1402)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Rand is a set of crates supporting (pseudo-)random generators:

With broad support for random value generation and random processes:

- [`Standard`](https://docs.rs/rand/latest/rand/distributions/struct.Standard.html) random value sampling,
- [`StandardUniform`](https://docs.rs/rand/latest/rand/distributions/struct.StandardUniform.html) random value sampling,
[`Uniform`](https://docs.rs/rand/latest/rand/distributions/struct.Uniform.html)-ranged value sampling
and [more](https://docs.rs/rand/latest/rand/distr/index.html)
- Samplers for a large number of non-uniform random number distributions via our own
Expand Down
6 changes: 3 additions & 3 deletions benches/benches/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//! Generating/filling arrays and iterators of output

use criterion::{criterion_group, criterion_main, Criterion};
use rand::distr::Standard;
use rand::distr::StandardUniform;
use rand::prelude::*;
use rand_pcg::Pcg64Mcg;

Expand All @@ -36,7 +36,7 @@ pub fn bench(c: &mut Criterion) {
g.bench_function("u16_sample_iter", |b| {
let mut rng = Pcg64Mcg::from_rng(&mut rand::rng());
b.iter(|| {
let v: Vec<u16> = Standard.sample_iter(&mut rng).take(512).collect();
let v: Vec<u16> = StandardUniform.sample_iter(&mut rng).take(512).collect();
v
});
});
Expand Down Expand Up @@ -70,7 +70,7 @@ pub fn bench(c: &mut Criterion) {
g.bench_function("u64_sample_iter", |b| {
let mut rng = Pcg64Mcg::from_rng(&mut rand::rng());
b.iter(|| {
let v: Vec<u64> = Standard.sample_iter(&mut rng).take(128).collect();
let v: Vec<u64> = StandardUniform.sample_iter(&mut rng).take(128).collect();
v
});
});
Expand Down
2 changes: 1 addition & 1 deletion benches/benches/bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn bench(c: &mut Criterion) {

g.bench_function("standard", |b| {
let mut rng = Pcg32::from_rng(&mut rand::rng());
b.iter(|| rng.sample::<bool, _>(rand::distr::Standard))
b.iter(|| rng.sample::<bool, _>(rand::distr::StandardUniform))
});

g.bench_function("const", |b| {
Expand Down
2 changes: 1 addition & 1 deletion benches/benches/generators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ pub fn init_from_seed(c: &mut Criterion) {

fn bench<R: SeedableRng>(g: &mut BenchmarkGroup<WallTime>, name: &str)
where
rand::distr::Standard: Distribution<<R as SeedableRng>::Seed>,
rand::distr::StandardUniform: Distribution<<R as SeedableRng>::Seed>,
{
g.bench_function(name, |b| {
let mut rng = Pcg32::from_os_rng();
Expand Down
6 changes: 3 additions & 3 deletions benches/benches/standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use core::time::Duration;
use criterion::measurement::WallTime;
use criterion::{criterion_group, criterion_main, BenchmarkGroup, Criterion};
use rand::distr::{Alphanumeric, Standard};
use rand::distr::{Alphanumeric, StandardUniform};
use rand::prelude::*;
use rand_distr::{Open01, OpenClosed01};
use rand_pcg::Pcg64Mcg;
Expand All @@ -34,14 +34,14 @@ where
}

pub fn bench(c: &mut Criterion) {
let mut g = c.benchmark_group("Standard");
let mut g = c.benchmark_group("StandardUniform");
g.sample_size(1000);
g.warm_up_time(Duration::from_millis(500));
g.measurement_time(Duration::from_millis(1000));

macro_rules! do_ty {
($t:ty) => {
bench_ty::<$t, Standard>(&mut g, stringify!($t));
bench_ty::<$t, StandardUniform>(&mut g, stringify!($t));
};
($t:ty, $($tt:ty),*) => {
do_ty!($t);
Expand Down
12 changes: 6 additions & 6 deletions rand_distr/src/cauchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

//! The Cauchy distribution `Cauchy(x₀, γ)`.

use crate::{Distribution, Standard};
use crate::{Distribution, StandardUniform};
use core::fmt;
use num_traits::{Float, FloatConst};
use rand::Rng;
Expand Down Expand Up @@ -58,7 +58,7 @@ use rand::Rng;
pub struct Cauchy<F>
where
F: Float + FloatConst,
Standard: Distribution<F>,
StandardUniform: Distribution<F>,
{
median: F,
scale: F,
Expand All @@ -85,7 +85,7 @@ impl std::error::Error for Error {}
impl<F> Cauchy<F>
where
F: Float + FloatConst,
Standard: Distribution<F>,
StandardUniform: Distribution<F>,
{
/// Construct a new `Cauchy` with the given shape parameters
/// `median` the peak location and `scale` the scale factor.
Expand All @@ -100,11 +100,11 @@ where
impl<F> Distribution<F> for Cauchy<F>
where
F: Float + FloatConst,
Standard: Distribution<F>,
StandardUniform: Distribution<F>,
{
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> F {
// sample from [0, 1)
let x = Standard.sample(rng);
let x = StandardUniform.sample(rng);
// get standard cauchy random number
// note that π/2 is not exactly representable, even if x=0.5 the result is finite
let comp_dev = (F::PI() * x).tan();
Expand Down Expand Up @@ -166,7 +166,7 @@ mod test {
fn value_stability() {
fn gen_samples<F: Float + FloatConst + fmt::Debug>(m: F, s: F, buf: &mut [F])
where
Standard: Distribution<F>,
StandardUniform: Distribution<F>,
{
let distr = Cauchy::new(m, s).unwrap();
let mut rng = crate::test::rng(353);
Expand Down
8 changes: 4 additions & 4 deletions rand_distr/src/inverse_gaussian.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! The inverse Gaussian distribution `IG(μ, λ)`.

use crate::{Distribution, Standard, StandardNormal};
use crate::{Distribution, StandardNormal, StandardUniform};
use core::fmt;
use num_traits::Float;
use rand::Rng;
Expand Down Expand Up @@ -53,7 +53,7 @@ pub struct InverseGaussian<F>
where
F: Float,
StandardNormal: Distribution<F>,
Standard: Distribution<F>,
StandardUniform: Distribution<F>,
{
mean: F,
shape: F,
Expand All @@ -63,7 +63,7 @@ impl<F> InverseGaussian<F>
where
F: Float,
StandardNormal: Distribution<F>,
Standard: Distribution<F>,
StandardUniform: Distribution<F>,
{
/// Construct a new `InverseGaussian` distribution with the given mean and
/// shape.
Expand All @@ -85,7 +85,7 @@ impl<F> Distribution<F> for InverseGaussian<F>
where
F: Float,
StandardNormal: Distribution<F>,
Standard: Distribution<F>,
StandardUniform: Distribution<F>,
{
#[allow(clippy::many_single_char_names)]
fn sample<R>(&self, rng: &mut R) -> F
Expand Down
4 changes: 2 additions & 2 deletions rand_distr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
//! The following are re-exported:
//!
//! - The [`Distribution`] trait and [`DistIter`] helper type
//! - The [`Standard`], [`Alphanumeric`], [`Uniform`], [`OpenClosed01`],
//! - The [`StandardUniform`], [`Alphanumeric`], [`Uniform`], [`OpenClosed01`],
//! [`Open01`], [`Bernoulli`], and [`WeightedIndex`] distributions
//!
//! ## Distributions
Expand Down Expand Up @@ -95,7 +95,7 @@ use rand::Rng;

pub use rand::distr::{
uniform, Alphanumeric, Bernoulli, BernoulliError, DistIter, Distribution, Open01, OpenClosed01,
Standard, Uniform,
StandardUniform, Uniform,
};

pub use self::beta::{Beta, Error as BetaError};
Expand Down
8 changes: 4 additions & 4 deletions rand_distr/src/normal_inverse_gaussian.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Distribution, InverseGaussian, Standard, StandardNormal};
use crate::{Distribution, InverseGaussian, StandardNormal, StandardUniform};
use core::fmt;
use num_traits::Float;
use rand::Rng;
Expand Down Expand Up @@ -54,7 +54,7 @@ pub struct NormalInverseGaussian<F>
where
F: Float,
StandardNormal: Distribution<F>,
Standard: Distribution<F>,
StandardUniform: Distribution<F>,
{
beta: F,
inverse_gaussian: InverseGaussian<F>,
Expand All @@ -64,7 +64,7 @@ impl<F> NormalInverseGaussian<F>
where
F: Float,
StandardNormal: Distribution<F>,
Standard: Distribution<F>,
StandardUniform: Distribution<F>,
{
/// Construct a new `NormalInverseGaussian` distribution with the given alpha (tail heaviness) and
/// beta (asymmetry) parameters.
Expand Down Expand Up @@ -94,7 +94,7 @@ impl<F> Distribution<F> for NormalInverseGaussian<F>
where
F: Float,
StandardNormal: Distribution<F>,
Standard: Distribution<F>,
StandardUniform: Distribution<F>,
{
fn sample<R>(&self, rng: &mut R) -> F
where
Expand Down
14 changes: 7 additions & 7 deletions rand_distr/src/poisson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

//! The Poisson distribution `Poisson(λ)`.

use crate::{Cauchy, Distribution, Standard};
use crate::{Cauchy, Distribution, StandardUniform};
use core::fmt;
use num_traits::{Float, FloatConst};
use rand::Rng;
Expand Down Expand Up @@ -55,7 +55,7 @@ use rand::Rng;
pub struct Poisson<F>(Method<F>)
where
F: Float + FloatConst,
Standard: Distribution<F>;
StandardUniform: Distribution<F>;

/// Error type returned from [`Poisson::new`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -130,7 +130,7 @@ enum Method<F> {
impl<F> Poisson<F>
where
F: Float + FloatConst,
Standard: Distribution<F>,
StandardUniform: Distribution<F>,
{
/// Construct a new `Poisson` with the given shape parameter
/// `lambda`.
Expand Down Expand Up @@ -172,7 +172,7 @@ where
impl<F> Distribution<F> for KnuthMethod<F>
where
F: Float + FloatConst,
Standard: Distribution<F>,
StandardUniform: Distribution<F>,
{
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> F {
let mut result = F::one();
Expand All @@ -188,7 +188,7 @@ where
impl<F> Distribution<F> for RejectionMethod<F>
where
F: Float + FloatConst,
Standard: Distribution<F>,
StandardUniform: Distribution<F>,
{
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> F {
// The algorithm from Numerical Recipes in C
Expand Down Expand Up @@ -238,7 +238,7 @@ where
impl<F> Distribution<F> for Poisson<F>
where
F: Float + FloatConst,
Standard: Distribution<F>,
StandardUniform: Distribution<F>,
{
#[inline]
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> F {
Expand All @@ -255,7 +255,7 @@ mod test {

fn test_poisson_avg_gen<F: Float + FloatConst>(lambda: F, tol: F)
where
Standard: Distribution<F>,
StandardUniform: Distribution<F>,
{
let poisson = Poisson::new(lambda).unwrap();
let mut rng = crate::test::rng(123);
Expand Down
10 changes: 5 additions & 5 deletions rand_distr/src/triangular.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// except according to those terms.
//! The triangular distribution.

use crate::{Distribution, Standard};
use crate::{Distribution, StandardUniform};
use core::fmt;
use num_traits::Float;
use rand::Rng;
Expand Down Expand Up @@ -43,7 +43,7 @@ use rand::Rng;
pub struct Triangular<F>
where
F: Float,
Standard: Distribution<F>,
StandardUniform: Distribution<F>,
{
min: F,
max: F,
Expand Down Expand Up @@ -76,7 +76,7 @@ impl std::error::Error for TriangularError {}
impl<F> Triangular<F>
where
F: Float,
Standard: Distribution<F>,
StandardUniform: Distribution<F>,
{
/// Set up the Triangular distribution with defined `min`, `max` and `mode`.
#[inline]
Expand All @@ -94,11 +94,11 @@ where
impl<F> Distribution<F> for Triangular<F>
where
F: Float,
Standard: Distribution<F>,
StandardUniform: Distribution<F>,
{
#[inline]
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> F {
let f: F = rng.sample(Standard);
let f: F = rng.sample(StandardUniform);
let diff_mode_min = self.mode - self.min;
let range = self.max - self.min;
let f_range = f * range;
Expand Down
10 changes: 5 additions & 5 deletions rand_distr/src/zeta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

//! The Zeta distribution.

use crate::{Distribution, Standard};
use crate::{Distribution, StandardUniform};
use core::fmt;
use num_traits::Float;
use rand::{distr::OpenClosed01, Rng};
Expand Down Expand Up @@ -68,7 +68,7 @@ use rand::{distr::OpenClosed01, Rng};
pub struct Zeta<F>
where
F: Float,
Standard: Distribution<F>,
StandardUniform: Distribution<F>,
OpenClosed01: Distribution<F>,
{
s_minus_1: F,
Expand Down Expand Up @@ -96,7 +96,7 @@ impl std::error::Error for Error {}
impl<F> Zeta<F>
where
F: Float,
Standard: Distribution<F>,
StandardUniform: Distribution<F>,
OpenClosed01: Distribution<F>,
{
/// Construct a new `Zeta` distribution with given `s` parameter.
Expand All @@ -117,7 +117,7 @@ where
impl<F> Distribution<F> for Zeta<F>
where
F: Float,
Standard: Distribution<F>,
StandardUniform: Distribution<F>,
OpenClosed01: Distribution<F>,
{
#[inline]
Expand All @@ -135,7 +135,7 @@ where

let t = (F::one() + F::one() / x).powf(self.s_minus_1);

let v = rng.sample(Standard);
let v = rng.sample(StandardUniform);
if v * x * (t - F::one()) * self.b <= t * (self.b - F::one()) {
return x;
}
Expand Down
Loading