Skip to content

Commit d726d19

Browse files
authored
Merge pull request #172 from martinfrances107/deprecated_rng
rng: fix 2 deprecated methods
2 parents 22428e2 + ed42766 commit d726d19

9 files changed

Lines changed: 55 additions & 55 deletions

examples/basic_usage.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ fn noisy_icp_example() -> Result<(), Box<dyn std::error::Error>> {
153153
}
154154

155155
// Create target points with known transformation + noise
156-
let mut rng = rand::thread_rng();
156+
let mut rng = rand::rng();
157157
for point in &source.points {
158158
let transformed = transform * point;
159159
// Add Gaussian noise

examples/comprehensive_gpu_example.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
393393
}
394394

395395
fn generate_sample_data() -> (PointCloud<Point3f>, PointCloud<Point3f>, Vec<f32>) {
396-
let mut rng = rand::thread_rng();
396+
let mut rng = rand::rng();
397397

398398
// Generate first point cloud (sphere)
399399
let mut source_points = Vec::new();

examples/euclidean_cluster_example.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! with multiple spatially-separated object blobs, as implemented for issue #95.
55
66
use rand::prelude::*;
7-
use rand::thread_rng;
7+
use rand::rng;
88
use threecrate_algorithms::{
99
extract_euclidean_clusters, extract_euclidean_clusters_parallel, EuclideanClusterConfig,
1010
};
@@ -67,7 +67,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
6767
/// Three sphere-shaped blobs at well-separated positions
6868
fn create_three_blobs() -> PointCloud<Point3f> {
6969
let mut cloud = PointCloud::new();
70-
let mut rng = thread_rng();
70+
let mut rng = rng();
7171

7272
let blobs = [
7373
(Point3f::new(0.0, 0.0, 0.0), 0.4_f32, 500_usize),
@@ -94,7 +94,7 @@ fn create_three_blobs() -> PointCloud<Point3f> {
9494
/// A denser scene with four blobs
9595
fn create_dense_scene() -> PointCloud<Point3f> {
9696
let mut cloud = PointCloud::new();
97-
let mut rng = thread_rng();
97+
let mut rng = rng();
9898

9999
let blobs = [
100100
(Point3f::new(0.0, 0.0, 0.0), 0.5_f32, 1000_usize),
@@ -122,7 +122,7 @@ fn create_dense_scene() -> PointCloud<Point3f> {
122122
/// Two large blobs plus several tiny noise blobs that should be filtered out
123123
fn create_cloud_with_noise_blobs() -> PointCloud<Point3f> {
124124
let mut cloud = PointCloud::new();
125-
let mut rng = thread_rng();
125+
let mut rng = rng();
126126

127127
// Large blobs
128128
for (center, radius, count) in [

examples/gpu_example.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ fn create_sample_point_cloud(num_points: usize, offset: f32) -> PointCloud<Point
111111
/// Add random outliers to a point cloud for testing filtering
112112
fn add_outliers(cloud: &mut PointCloud<Point3f>, num_outliers: usize) {
113113
use rand::Rng;
114-
let mut rng = rand::thread_rng();
114+
let mut rng = rand::rng();
115115

116116
for _ in 0..num_outliers {
117117
// Add points far from the main cluster

examples/k_nearest_neighbors_example.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
2626
println!("Created point cloud with {} points", cloud.len());
2727

2828
// Add some random points for more interesting results
29-
let mut rng = rand::thread_rng();
29+
let mut rng = rand::rng();
3030
for _ in 0..20 {
3131
cloud.push(Point3f::new(
3232
rng.random_range(-2.0..7.0),

examples/ransac_plane_example.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! on noisy planar point clouds, as requested in GitHub issue #4.
55
66
use rand::prelude::*;
7-
use rand::thread_rng;
7+
use rand::rng;
88
use threecrate_algorithms::{plane_segmentation_ransac, segment_plane_ransac};
99
use threecrate_core::{Point3f, PointCloud};
1010

@@ -99,7 +99,7 @@ fn create_simple_planar_cloud() -> PointCloud<Point3f> {
9999
/// Create a noisy planar point cloud
100100
fn create_noisy_planar_cloud() -> PointCloud<Point3f> {
101101
let mut cloud = PointCloud::new();
102-
let mut rng = thread_rng();
102+
let mut rng = rng();
103103

104104
// Create a 20x20 grid on the XY plane with noise
105105
for i in 0..20 {
@@ -125,7 +125,7 @@ fn create_noisy_planar_cloud() -> PointCloud<Point3f> {
125125
/// Create a tilted plane with outliers
126126
fn create_tilted_plane_with_outliers() -> PointCloud<Point3f> {
127127
let mut cloud = PointCloud::new();
128-
let mut rng = thread_rng();
128+
let mut rng = rng();
129129

130130
// Create a tilted plane: x + y + z = 0
131131
for i in 0..15 {
@@ -157,7 +157,7 @@ fn create_tilted_plane_with_outliers() -> PointCloud<Point3f> {
157157
/// Create multiple planes (for demonstrating single plane detection)
158158
fn create_multiple_planes() -> PointCloud<Point3f> {
159159
let mut cloud = PointCloud::new();
160-
let mut rng = thread_rng();
160+
let mut rng = rng();
161161

162162
// First plane: z = 0 (largest)
163163
for i in 0..25 {

threecrate-algorithms/src/ground_segmentation.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -430,9 +430,9 @@ mod tests {
430430
// Flat ground at z = -sensor_height over a 60×60 area, with mild noise.
431431
let z_ground = -sensor_height;
432432
for _ in 0..8000 {
433-
let x: f32 = rng.gen_range(-30.0..30.0);
434-
let y: f32 = rng.gen_range(-30.0..30.0);
435-
let z = z_ground + rng.gen_range(-0.02..0.02);
433+
let x: f32 = rng.random_range(-30.0..30.0);
434+
let y: f32 = rng.random_range(-30.0..30.0);
435+
let z = z_ground + rng.random_range(-0.02..0.02);
436436
// Skip points right under the sensor (too close to origin).
437437
if x * x + y * y < 0.25 {
438438
continue;
@@ -443,16 +443,16 @@ mod tests {
443443
if with_obstacles {
444444
// Tall vertical "wall" / obstacle cluster.
445445
for _ in 0..1500 {
446-
let x = 8.0 + rng.gen_range(-0.4..0.4);
447-
let y = rng.gen_range(-3.0..3.0);
448-
let z = z_ground + rng.gen_range(0.5..3.0);
446+
let x = 8.0 + rng.random_range(-0.4..0.4);
447+
let y = rng.random_range(-3.0..3.0);
448+
let z = z_ground + rng.random_range(0.5..3.0);
449449
cloud.push(Point3f::new(x, y, z));
450450
}
451451
// A pole.
452452
for _ in 0..400 {
453-
let x = -5.0 + rng.gen_range(-0.1..0.1);
454-
let y = -5.0 + rng.gen_range(-0.1..0.1);
455-
let z = z_ground + rng.gen_range(0.0..4.0);
453+
let x = -5.0 + rng.random_range(-0.1..0.1);
454+
let y = -5.0 + rng.random_range(-0.1..0.1);
455+
let z = z_ground + rng.random_range(0.0..4.0);
456456
cloud.push(Point3f::new(x, y, z));
457457
}
458458
}

threecrate-algorithms/src/nearest_neighbor.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -549,15 +549,15 @@ mod tests {
549549

550550
#[test]
551551
fn test_random_points() {
552-
let mut rng = rand::thread_rng();
552+
let mut rng = rand::rng();
553553
let mut points = Vec::new();
554554

555555
// Generate 100 random points
556556
for _ in 0..100 {
557557
points.push(Point3f::new(
558-
rng.gen_range(-10.0..10.0),
559-
rng.gen_range(-10.0..10.0),
560-
rng.gen_range(-10.0..10.0),
558+
rng.random_range(-10.0..10.0),
559+
rng.random_range(-10.0..10.0),
560+
rng.random_range(-10.0..10.0),
561561
));
562562
}
563563

@@ -567,13 +567,13 @@ mod tests {
567567
// Test multiple random queries
568568
for _ in 0..10 {
569569
let query = Point3f::new(
570-
rng.gen_range(-5.0..5.0),
571-
rng.gen_range(-5.0..5.0),
572-
rng.gen_range(-5.0..5.0),
570+
rng.random_range(-5.0..5.0),
571+
rng.random_range(-5.0..5.0),
572+
rng.random_range(-5.0..5.0),
573573
);
574574

575-
let k = rng.gen_range(1..=10);
576-
let radius = rng.gen_range(1.0..5.0);
575+
let k = rng.random_range(1..=10);
576+
let radius = rng.random_range(1.0..5.0);
577577

578578
let mut kdtree_knn = kdtree.find_k_nearest(&query, k);
579579
let mut brute_knn = brute_force.find_k_nearest(&query, k);
@@ -627,15 +627,15 @@ mod tests {
627627

628628
#[test]
629629
fn test_performance_comparison() {
630-
let mut rng = rand::thread_rng();
630+
let mut rng = rand::rng();
631631
let mut points = Vec::new();
632632

633633
// Generate 1000 random points for performance test
634634
for _ in 0..1000 {
635635
points.push(Point3f::new(
636-
rng.gen_range(-10.0..10.0),
637-
rng.gen_range(-10.0..10.0),
638-
rng.gen_range(-10.0..10.0),
636+
rng.random_range(-10.0..10.0),
637+
rng.random_range(-10.0..10.0),
638+
rng.random_range(-10.0..10.0),
639639
));
640640
}
641641

threecrate-algorithms/src/segmentation.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use crate::KdTree;
44
use nalgebra::Vector4;
55
use rand::prelude::*;
6-
use rand::thread_rng;
6+
use rand::rng;
77
use rayon::prelude::*;
88
use std::collections::{HashSet, VecDeque};
99
use threecrate_core::NearestNeighborSearch;
@@ -136,7 +136,7 @@ pub fn segment_plane(
136136
}
137137

138138
let points = &cloud.points;
139-
let mut rng = thread_rng();
139+
let mut rng = rng();
140140
let mut best_model: Option<PlaneModel> = None;
141141
let mut best_inliers = Vec::new();
142142
let mut best_score = 0;
@@ -145,7 +145,7 @@ pub fn segment_plane(
145145
// Randomly sample 3 points
146146
let mut indices = HashSet::new();
147147
while indices.len() < 3 {
148-
indices.insert(rng.gen_range(0..points.len()));
148+
indices.insert(rng.random_range(0..points.len()));
149149
}
150150
let indices: Vec<usize> = indices.into_iter().collect();
151151

@@ -218,12 +218,12 @@ pub fn segment_plane_parallel(
218218
let results: Vec<_> = (0..max_iters)
219219
.into_par_iter()
220220
.filter_map(|_| {
221-
let mut rng = thread_rng();
221+
let mut rng = rng();
222222

223223
// Randomly sample 3 points
224224
let mut indices = HashSet::new();
225225
while indices.len() < 3 {
226-
indices.insert(rng.gen_range(0..points.len()));
226+
indices.insert(rng.random_range(0..points.len()));
227227
}
228228
let indices: Vec<usize> = indices.into_iter().collect();
229229

@@ -711,23 +711,23 @@ mod tests {
711711
fn test_segment_plane_ransac_noisy() {
712712
// Create a point cloud with noisy planar points
713713
let mut cloud = PointCloud::new();
714-
let mut rng = thread_rng();
714+
let mut rng = rng();
715715

716716
// Add points on XY plane (z=0) with noise
717717
for i in 0..20 {
718718
for j in 0..20 {
719719
let x = i as f32;
720720
let y = j as f32;
721-
let z = rng.gen_range(-0.05..0.05); // Add noise to z coordinate
721+
let z = rng.random_range(-0.05..0.05); // Add noise to z coordinate
722722
cloud.push(Point3f::new(x, y, z));
723723
}
724724
}
725725

726726
// Add some outliers
727727
for _ in 0..20 {
728-
let x = rng.gen_range(0.0..20.0);
729-
let y = rng.gen_range(0.0..20.0);
730-
let z = rng.gen_range(1.0..5.0); // Outliers above the plane
728+
let x = rng.random_range(0.0..20.0);
729+
let y = rng.random_range(0.0..20.0);
730+
let z = rng.random_range(1.0..5.0); // Outliers above the plane
731731
cloud.push(Point3f::new(x, y, z));
732732
}
733733

@@ -764,7 +764,7 @@ mod tests {
764764
fn test_segment_plane_ransac_tilted_plane() {
765765
// Create a tilted plane (not aligned with coordinate axes)
766766
let mut cloud = PointCloud::new();
767-
let mut rng = thread_rng();
767+
let mut rng = rng();
768768

769769
// Create a tilted plane: x + y + z = 0
770770
for i in 0..15 {
@@ -774,19 +774,19 @@ mod tests {
774774
let z = -(x + y); // Points on the plane x + y + z = 0
775775

776776
// Add some noise
777-
let noise_x = rng.gen_range(-0.02..0.02);
778-
let noise_y = rng.gen_range(-0.02..0.02);
779-
let noise_z = rng.gen_range(-0.02..0.02);
777+
let noise_x = rng.random_range(-0.02..0.02);
778+
let noise_y = rng.random_range(-0.02..0.02);
779+
let noise_z = rng.random_range(-0.02..0.02);
780780

781781
cloud.push(Point3f::new(x + noise_x, y + noise_y, z + noise_z));
782782
}
783783
}
784784

785785
// Add outliers
786786
for _ in 0..30 {
787-
let x = rng.gen_range(0.0..15.0);
788-
let y = rng.gen_range(0.0..15.0);
789-
let z = rng.gen_range(5.0..10.0); // Outliers above the plane
787+
let x = rng.random_range(0.0..15.0);
788+
let y = rng.random_range(0.0..15.0);
789+
let z = rng.random_range(5.0..10.0); // Outliers above the plane
790790
cloud.push(Point3f::new(x, y, z));
791791
}
792792

@@ -873,12 +873,12 @@ mod tests {
873873
// ---- Euclidean cluster extraction tests ----
874874

875875
fn make_sphere_cloud(center: Point3f, radius: f32, count: usize) -> Vec<Point3f> {
876-
let mut rng = thread_rng();
876+
let mut rng = rng();
877877
let mut pts = Vec::with_capacity(count);
878878
while pts.len() < count {
879-
let x: f32 = rng.gen_range(-radius..radius);
880-
let y: f32 = rng.gen_range(-radius..radius);
881-
let z: f32 = rng.gen_range(-radius..radius);
879+
let x: f32 = rng.random_range(-radius..radius);
880+
let y: f32 = rng.random_range(-radius..radius);
881+
let z: f32 = rng.random_range(-radius..radius);
882882
if x * x + y * y + z * z <= radius * radius {
883883
pts.push(Point3f::new(center.x + x, center.y + y, center.z + z));
884884
}

0 commit comments

Comments
 (0)