Skip to content

Commit 1c7af6c

Browse files
authored
Merge pull request #123 from oligamiq/main
Update rand
2 parents 44a58e2 + 9f3cac0 commit 1c7af6c

File tree

3 files changed

+96
-40
lines changed

3 files changed

+96
-40
lines changed

Cargo.lock

+68-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+5-3
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ num-traits = { version = "0.2.14", default-features = false, features = [
3535
zerocopy = { version = "0.8", default-features = false, features = [
3636
"derive",
3737
], optional = true }
38-
rand = { version = "0.8.5", default-features = false, optional = true }
39-
rand_distr = { version = "0.4.3", default-features = false, optional = true }
38+
rand = { version = "0.9.0", default-features = false, features = [
39+
"thread_rng",
40+
], optional = true }
41+
rand_distr = { version = "0.5.0", default-features = false, optional = true }
4042
rkyv = { version = "0.7", optional = true }
4143
arbitrary = { version = "1.3.2", features = ["derive"], optional = true }
4244

@@ -47,7 +49,7 @@ crunchy = "0.2.2"
4749
criterion = "0.4.0"
4850
quickcheck = "1.0"
4951
quickcheck_macros = "1.0"
50-
rand = "0.8.5"
52+
rand = "0.9.0"
5153
crunchy = "0.2.2"
5254

5355
[[bench]]

src/rand_distr.rs

+23-23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{bf16, f16};
22

3-
use rand::{distributions::Distribution, Rng};
3+
use rand::{distr::Distribution, Rng};
44
use rand_distr::uniform::UniformFloat;
55

66
macro_rules! impl_distribution_via_f32 {
@@ -13,13 +13,13 @@ macro_rules! impl_distribution_via_f32 {
1313
};
1414
}
1515

16-
impl_distribution_via_f32!(f16, rand_distr::Standard);
16+
impl_distribution_via_f32!(f16, rand_distr::StandardUniform);
1717
impl_distribution_via_f32!(f16, rand_distr::StandardNormal);
1818
impl_distribution_via_f32!(f16, rand_distr::Exp1);
1919
impl_distribution_via_f32!(f16, rand_distr::Open01);
2020
impl_distribution_via_f32!(f16, rand_distr::OpenClosed01);
2121

22-
impl_distribution_via_f32!(bf16, rand_distr::Standard);
22+
impl_distribution_via_f32!(bf16, rand_distr::StandardUniform);
2323
impl_distribution_via_f32!(bf16, rand_distr::StandardNormal);
2424
impl_distribution_via_f32!(bf16, rand_distr::Exp1);
2525
impl_distribution_via_f32!(bf16, rand_distr::Open01);
@@ -34,25 +34,25 @@ impl rand_distr::uniform::SampleUniform for f16 {
3434

3535
impl rand_distr::uniform::UniformSampler for Float16Sampler {
3636
type X = f16;
37-
fn new<B1, B2>(low: B1, high: B2) -> Self
37+
fn new<B1, B2>(low: B1, high: B2) -> Result<Self, rand_distr::uniform::Error>
3838
where
3939
B1: rand_distr::uniform::SampleBorrow<Self::X> + Sized,
4040
B2: rand_distr::uniform::SampleBorrow<Self::X> + Sized,
4141
{
42-
Self(UniformFloat::new(
42+
Ok(Self(UniformFloat::new(
4343
low.borrow().to_f32(),
4444
high.borrow().to_f32(),
45-
))
45+
)?))
4646
}
47-
fn new_inclusive<B1, B2>(low: B1, high: B2) -> Self
47+
fn new_inclusive<B1, B2>(low: B1, high: B2) -> Result<Self, rand_distr::uniform::Error>
4848
where
4949
B1: rand_distr::uniform::SampleBorrow<Self::X> + Sized,
5050
B2: rand_distr::uniform::SampleBorrow<Self::X> + Sized,
5151
{
52-
Self(UniformFloat::new_inclusive(
52+
Ok(Self(UniformFloat::new_inclusive(
5353
low.borrow().to_f32(),
5454
high.borrow().to_f32(),
55-
))
55+
)?))
5656
}
5757
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Self::X {
5858
f16::from_f32(self.0.sample(rng))
@@ -68,25 +68,25 @@ impl rand_distr::uniform::SampleUniform for bf16 {
6868

6969
impl rand_distr::uniform::UniformSampler for BFloat16Sampler {
7070
type X = bf16;
71-
fn new<B1, B2>(low: B1, high: B2) -> Self
71+
fn new<B1, B2>(low: B1, high: B2) -> Result<Self, rand_distr::uniform::Error>
7272
where
7373
B1: rand_distr::uniform::SampleBorrow<Self::X> + Sized,
7474
B2: rand_distr::uniform::SampleBorrow<Self::X> + Sized,
7575
{
76-
Self(UniformFloat::new(
76+
Ok(Self(UniformFloat::new(
7777
low.borrow().to_f32(),
7878
high.borrow().to_f32(),
79-
))
79+
)?))
8080
}
81-
fn new_inclusive<B1, B2>(low: B1, high: B2) -> Self
81+
fn new_inclusive<B1, B2>(low: B1, high: B2) -> Result<Self, rand_distr::uniform::Error>
8282
where
8383
B1: rand_distr::uniform::SampleBorrow<Self::X> + Sized,
8484
B2: rand_distr::uniform::SampleBorrow<Self::X> + Sized,
8585
{
86-
Self(UniformFloat::new_inclusive(
86+
Ok(Self(UniformFloat::new_inclusive(
8787
low.borrow().to_f32(),
8888
high.borrow().to_f32(),
89-
))
89+
)?))
9090
}
9191
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Self::X {
9292
bf16::from_f32(self.0.sample(rng))
@@ -98,26 +98,26 @@ mod tests {
9898
use super::*;
9999

100100
#[allow(unused_imports)]
101-
use rand::{thread_rng, Rng};
102-
use rand_distr::{Standard, StandardNormal, Uniform};
101+
use rand::{rng, Rng};
102+
use rand_distr::{StandardNormal, StandardUniform, Uniform};
103103

104104
#[test]
105105
fn test_sample_f16() {
106-
let mut rng = thread_rng();
107-
let _: f16 = rng.sample(Standard);
106+
let mut rng = rng();
107+
let _: f16 = rng.sample(StandardUniform);
108108
let _: f16 = rng.sample(StandardNormal);
109-
let _: f16 = rng.sample(Uniform::new(f16::from_f32(0.0), f16::from_f32(1.0)));
109+
let _: f16 = rng.sample(Uniform::new(f16::from_f32(0.0), f16::from_f32(1.0)).unwrap());
110110
#[cfg(feature = "num-traits")]
111111
let _: f16 =
112112
rng.sample(rand_distr::Normal::new(f16::from_f32(0.0), f16::from_f32(1.0)).unwrap());
113113
}
114114

115115
#[test]
116116
fn test_sample_bf16() {
117-
let mut rng = thread_rng();
118-
let _: bf16 = rng.sample(Standard);
117+
let mut rng = rng();
118+
let _: bf16 = rng.sample(StandardUniform);
119119
let _: bf16 = rng.sample(StandardNormal);
120-
let _: bf16 = rng.sample(Uniform::new(bf16::from_f32(0.0), bf16::from_f32(1.0)));
120+
let _: bf16 = rng.sample(Uniform::new(bf16::from_f32(0.0), bf16::from_f32(1.0)).unwrap());
121121
#[cfg(feature = "num-traits")]
122122
let _: bf16 =
123123
rng.sample(rand_distr::Normal::new(bf16::from_f32(0.0), bf16::from_f32(1.0)).unwrap());

0 commit comments

Comments
 (0)