diff --git a/src/lib.rs b/src/lib.rs index 7450ca2..1f4666b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,6 +32,11 @@ where let ef_search = builder.ef_search.unwrap_or(100); let ef_construction = builder.ef_construction.unwrap_or(100); let ml = builder.ml.unwrap_or_else(|| (M as f32).ln()); + let mut rng = match builder.seed { + Some(seed) => SmallRng::seed_from_u64(seed), + None => SmallRng::from_entropy(), + }; + #[cfg(feature = "indicatif")] let progress = builder.progress; #[cfg(feature = "indicatif")] @@ -57,7 +62,6 @@ where // progresses, while preserving randomness in each point's layer and insertion order. assert!(points.len() < u32::MAX as usize); - let mut rng = SmallRng::from_entropy(); let mut nodes = (0..points.len()) .map(|i| (LayerId::random(ml, &mut rng), i)) .collect::>(); @@ -402,6 +406,7 @@ pub struct Builder { ef_search: Option, ef_construction: Option, ml: Option, + seed: Option, #[cfg(feature = "indicatif")] progress: Option, } @@ -433,6 +438,14 @@ impl Builder { self } + /// Set the seed value for the random number generator used to generate a layer for each point + /// + /// If this value is left unset, a seed is generated from entropy (via `getrandom()`). + pub fn seed(mut self, seed: u64) -> Self { + self.seed = Some(seed); + self + } + /// A `ProgressBar` to track `Hnsw` construction progress #[cfg(feature = "indicatif")] pub fn progress(mut self, bar: ProgressBar) -> Self {