Skip to content

Commit cd86feb

Browse files
committed
fix: implement Normal sampling via Box-Muller using rand 0.10 primitives only
rand::distr::StandardNormal does not exist in rand 0.10 — StandardNormal is only in rand_distr, which conflicts with rand 0.10. Instead, implement sample_normal() using the Box-Muller transform: z = sqrt(-2 * ln(u1)) * cos(2π * u2) where u1,u2 ~ Uniform(0,1) This uses only rand::distr::Uniform and rand::Rng from rand 0.10, requiring no external crates.
1 parent 7024190 commit cd86feb

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

src/dataset/generator.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
//! # Dataset Generators
22
//!
33
use rand::distr::Distribution;
4-
use rand::distr::StandardNormal;
54
use rand::distr::Uniform;
65

76
use crate::dataset::Dataset;
87

9-
/// Sample from N(mean, std) using rand 0.10's StandardNormal
8+
/// Sample from N(mean, std) via Box-Muller transform using only rand 0.10
109
#[inline]
1110
fn sample_normal(mean: f32, std: f32, rng: &mut impl rand::Rng) -> f32 {
12-
mean + std * StandardNormal.sample(rng)
11+
let unit = Uniform::new(f32::EPSILON, 1.0f32).unwrap();
12+
let u1 = unit.sample(rng);
13+
let u2 = unit.sample(rng);
14+
let z = (-2.0 * u1.ln()).sqrt() * (2.0 * std::f32::consts::PI * u2).cos();
15+
mean + std * z
1316
}
1417

1518
/// Generate `num_centers` clusters of normally distributed points
@@ -22,7 +25,7 @@ pub fn make_blobs(
2225
let cluster_std = 1.0f32;
2326
let mut rng = rand::rng();
2427

25-
// Pre-compute cluster centers (mean values per feature)
28+
// Pre-compute cluster centers (one mean per feature per cluster)
2629
let centers: Vec<Vec<f32>> = (0..num_centers)
2730
.map(|_| {
2831
(0..num_features)

0 commit comments

Comments
 (0)