From f53c8ede3d9b26a7e64147c1505ebb34c4e1c114 Mon Sep 17 00:00:00 2001 From: Dilawar Singh Date: Wed, 18 Sep 2024 14:50:50 +0530 Subject: [PATCH] seed the rng --- Cargo.toml | 18 +++++++++--------- src/lib.rs | 11 ++++++++--- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index dd14cce..62d14d4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,15 +1,15 @@ [package] -name = "cron-with-randomness" -version = "0.0.2" +name = "cron-within-interval" +version = "0.1.0" edition = "2021" [dependencies] -anyhow = "1.0.75" -chrono = "0.4.31" -cron = "0.12.0" -lazy_static = "1.4.0" -rand = "0.8.5" -regex = "1.10.2" +anyhow = "1.0" +chrono = "0.4" +cron = "0.12" +lazy_static = "1.5" +rand = "0.8" +regex = "1.10" [dev-dependencies] -simple_accumulator = "0.5.0" +simple_accumulator = "0.7.0" diff --git a/src/lib.rs b/src/lib.rs index f12fc1e..e40419c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,10 @@ -//! cron-with-randomness +//! cron-within-interval //! //! Extended cron shorthands that support sampling from given interval. In addition to standard //! expression supported by excellent crate cron, we support following type of expressions. //! +//! The random number is seeded so you always get the same sequence. +//! //! - `@daily{h=9-17}` means run once between 9am and 5pm chosen randomly. //! - `@daily{h=9-12,h=15-20}` means run once between 9am and 12pm or between 3pm and 8pm. //! @@ -21,6 +23,9 @@ use cron::Schedule; use rand::{seq::SliceRandom, Rng}; use regex::Regex; +/// Global seed fro rngs. +const SEED: u64 = 1443; + lazy_static::lazy_static! { static ref RE: Regex = Regex::new( r"(?x) @@ -107,7 +112,7 @@ impl CronWithRandomness { where Z: chrono::TimeZone, { - let mut rng = rand::thread_rng(); + let mut rng = rand::StdRng::seed_from_u64(SEED); let mut result_datetime = datetime.clone(); // pick a random minute. We have to reduce one hour from the hour range after this. @@ -155,7 +160,7 @@ impl FromStr for Interval { impl Interval { /// Generate a random value between the interval fn random(&self) -> i16 { - let mut rng = rand::thread_rng(); + let mut rng = rand::StdRng::seed_from_u64(SEED); // high is exclusive rng.gen_range(self.0..self.1) }