This library provides a set of low-discrepancy sequence generators that create sequences of numbers that are more evenly distributed than random numbers. These sequences are particularly useful in various fields such as computer graphics, numerical integration, and Monte Carlo simulations.
All sequence generators are thread-safe, using atomic operations for internal state management, making them safe to use in concurrent environments.
- van der Corput sequence: Base implementation for 1D sequences
- Halton sequence: 2D and N-dimensional sequences using different bases
- Geometric sequences:
Circle: Points on the unit circleDisk: Points in the unit diskSphere: Points on the unit sphereSphere3Hopf: Points on the 3-sphere using Hopf fibration
- N-dimensional spheres:
Sphere3: Points on 3-sphere (4D)SphereN: Points on n-sphere for arbitrary dimensions
- Integer sequences: Integer versions of van der Corput and Halton sequences
- Thread-safe: All generators use atomic operations for safe concurrent access
- Prime table: First 1000 prime numbers for use as sequence bases
Add this to your Cargo.toml:
[dependencies]
lds-gen = "0.1.0"use lds_gen::VdCorput;
let mut vgen = VdCorput::new(2);
vgen.reseed(0);
println!("First value: {}", vgen.pop()); // 0.5
println!("Second value: {}", vgen.pop()); // 0.25use lds_gen::Halton;
let mut hgen = Halton::new([2, 3]);
hgen.reseed(0);
let point = hgen.pop();
println!("Point: {:?}", point); // [0.5, 0.3333333333333333]use lds_gen::Sphere;
let mut sgen = Sphere::new([2, 3]);
sgen.reseed(0);
let point = sgen.pop();
println!("Sphere point: {:?}", point); // Point on unit sphereuse lds_gen::sphere_n::{Sphere3, SphereN, SphereGen};
// 3-sphere (4D)
let mut sgen3 = Sphere3::new(&[2, 3, 5]);
sgen3.reseed(0);
let point3 = sgen3.pop();
println!("3-sphere point: {:?}", point3); // 4D point on unit 3-sphere
// n-sphere (5D)
let mut sgen_n = SphereN::new(&[2, 3, 5, 7]);
sgen_n.reseed(0);
let point_n = sgen_n.pop();
println!("n-sphere point: {:?}", point_n); // 5D point on unit 4-sphereThe sphere generators use thread-safe lazy initialization and caching for optimal performance in concurrent environments.
use lds_gen::ilds::{VdCorput, Halton};
// Integer van der Corput
let mut ivdc = VdCorput::new(2, 10);
ivdc.reseed(0);
println!("Integer value: {}", ivdc.pop()); // 512
// Integer Halton
let mut ihalton = Halton::new([2, 3], [11, 7]);
ihalton.reseed(0);
let int_point = ihalton.pop();
println!("Integer point: {:?}", int_point); // [1024, 729]All sequence generators are thread-safe and can be safely shared across threads:
use std::sync::Arc;
use std::thread;
use lds_gen::Halton;
let halton = Arc::new(Halton::new([2, 3]));
halton.reseed(0);
let mut handles = vec![];
for _ in 0..4 {
let halton_clone = Arc::clone(&halton);
let handle = thread::spawn(move || {
// Each thread safely generates points
let point = halton_clone.pop();
println!("Thread point: {:?}", point);
});
handles.push(handle);
}
for handle in handles {
handle.join().unwrap();
}VdCorput: van der Corput sequence generator (thread-safe)Halton: 2D Halton sequence generator (thread-safe)Circle: Unit circle sequence generator (thread-safe)Disk: Unit disk sequence generator (thread-safe)Sphere: Unit sphere sequence generator (thread-safe)Sphere3Hopf: 3-sphere sequence generator using Hopf coordinates (thread-safe)HaltonN: N-dimensional Halton sequence generator (thread-safe)
sphere_n::SphereGen: Thread-safe trait for sphere generators (implements Send)sphere_n::Sphere3: Thread-safe 3-sphere (4D) sequence generatorsphere_n::SphereN: Thread-safe N-sphere sequence generator for arbitrary dimensions
ilds::VdCorput: Integer van der Corput sequence generator (thread-safe)ilds::Halton: Integer 2D Halton sequence generator (thread-safe)
TWO_PI: Constant for 2ΟPRIME_TABLE: First 1000 prime numbers
See the examples directory for more usage examples.
Run the tests with:
cargo test- Install the rust toolchain in order to have cargo installed by following this guide.
- run
cargo install lds-rs
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
See CONTRIBUTING.md.