Skip to content

Commit

Permalink
seed the rng
Browse files Browse the repository at this point in the history
  • Loading branch information
dilawar committed Sep 18, 2024
1 parent 7441ae0 commit f53c8ed
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
18 changes: 9 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
11 changes: 8 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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.
//!
Expand All @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit f53c8ed

Please sign in to comment.