Skip to content

Commit

Permalink
chore: upgrade rand dependency to 0.9
Browse files Browse the repository at this point in the history
  • Loading branch information
robjtede committed Feb 17, 2025
1 parent 10ee2ee commit af50fd7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ ndarray = { default-features = false, optional = true, version = "0.15.6" }
num-traits = { default-features = false, features = ["i128"], version = "0.2" }
postgres = { default-features = false, optional = true, version = "0.19" }
proptest = { default-features = false, optional = true, features = ["std"], version = "1.0" }
rand = { default-features = false, optional = true, version = "0.8" }
rand = { default-features = false, optional = true, version = "0.9" }
rkyv = { default-features = false, features = ["size_32", "std"], optional = true, version = "0.7.42" }
rocket = { default-features = false, optional = true, version = "0.5.0-rc.3" }
rust_decimal_macros = { default-features = false, optional = true, version = "1.33" } # This needs to be the n-1 published version
Expand All @@ -43,7 +43,7 @@ bytes = { default-features = false, version = "1.0" }
criterion = { default-features = false, version = "0.5" }
csv = "1"
futures = { default-features = false, version = "0.3" }
rand = { default-features = false, features = ["getrandom"], version = "0.8" }
rand = { default-features = false, features = ["thread_rng"], version = "0.9" }
rust_decimal_macros = { default-features = false, version = "1.33" }
serde = { default-features = false, features = ["derive"], version = "1.0" }
serde_json = "1.0"
Expand Down
30 changes: 15 additions & 15 deletions src/rand.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::Decimal;
use rand::{
distributions::{
distr::{
uniform::{SampleBorrow, SampleUniform, UniformInt, UniformSampler},
Distribution, Standard,
Distribution, StandardUniform,
},
Rng,
};

impl Distribution<Decimal> for Standard {
impl Distribution<Decimal> for StandardUniform {
fn sample<R>(&self, rng: &mut R) -> Decimal
where
R: Rng + ?Sized,
Expand All @@ -16,7 +16,7 @@ impl Distribution<Decimal> for Standard {
rng.next_u32(),
rng.next_u32(),
rng.next_u32(),
rng.gen(),
rng.random(),
rng.next_u32(),
)
}
Expand Down Expand Up @@ -52,7 +52,7 @@ impl UniformSampler for DecimalSampler {
/// assert_eq!(random.scale(), 2);
/// ```
#[inline]
fn new<B1, B2>(low: B1, high: B2) -> Self
fn new<B1, B2>(low: B1, high: B2) -> Result<Self, rand::distr::uniform::Error>
where
B1: SampleBorrow<Self::X> + Sized,
B2: SampleBorrow<Self::X> + Sized,
Expand All @@ -79,7 +79,7 @@ impl UniformSampler for DecimalSampler {
/// assert_eq!(random.scale(), 2);
/// ```
#[inline]
fn new_inclusive<B1, B2>(low: B1, high: B2) -> Self
fn new_inclusive<B1, B2>(low: B1, high: B2) -> Result<Self, rand::distr::uniform::Error>
where
B1: SampleBorrow<Self::X> + Sized,
B2: SampleBorrow<Self::X> + Sized,
Expand All @@ -88,10 +88,10 @@ impl UniformSampler for DecimalSampler {

// Return our sampler, which contains an underlying i128 sampler so we
// outsource the actual randomness implementation.
Self {
mantissa_sampler: UniformInt::new_inclusive(low.mantissa(), high.mantissa()),
Ok(Self {
mantissa_sampler: UniformInt::new_inclusive(low.mantissa(), high.mantissa())?,
scale: low.scale(),
}
})
}

#[inline]
Expand Down Expand Up @@ -138,27 +138,27 @@ mod tests {

#[test]
fn has_random_decimal_instances() {
let mut rng = rand::rngs::OsRng;
let random: [Decimal; 32] = rng.gen();
let mut rng = rand::rng();
let random: [Decimal; 32] = rng.random();
assert!(random.windows(2).any(|slice| { slice[0] != slice[1] }));
}

#[test]
fn generates_within_range() {
let mut rng = rand::rngs::OsRng;
let mut rng = rand::rng();
for _ in 0..128 {
let random = rng.gen_range(dec!(1.00)..dec!(1.05));
let random = rng.random_range(dec!(1.00)..dec!(1.05));
assert!(random < dec!(1.05));
assert!(random >= dec!(1.00));
}
}

#[test]
fn generates_within_inclusive_range() {
let mut rng = rand::rngs::OsRng;
let mut rng = rand::rng();
let mut values: HashSet<Decimal> = HashSet::new();
for _ in 0..256 {
let random = rng.gen_range(dec!(1.00)..=dec!(1.01));
let random = rng.random_range(dec!(1.00)..=dec!(1.01));
// The scale is 2, so 1.00 and 1.01 are the only two valid choices.
assert!(random == dec!(1.00) || random == dec!(1.01));
values.insert(random);
Expand Down

0 comments on commit af50fd7

Please sign in to comment.