View on GitHub

lds-rs

🤏 Low-discrepancy Sequence Generation in Rust

🤏 lds-rs - Low-Discrepancy Sequence Generator in Rust

Crates.io Docs.rs CI codecov

Overview

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.

Features

Installation

Add this to your Cargo.toml:

[dependencies]
lds-gen = "0.1.0"

Usage

Basic Van der Corput Sequence

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.25

2D Halton Sequence

use lds_gen::Halton;

let mut hgen = Halton::new([2, 3]);
hgen.reseed(0);
let point = hgen.pop();
println!("Point: {:?}", point); // [0.5, 0.3333333333333333]

Points on a Sphere

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 sphere

N-Dimensional Spheres

use 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-sphere

The sphere generators use thread-safe lazy initialization and caching for optimal performance in concurrent environments.

Integer Sequences

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]

Thread-safe Usage

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();
}

API Reference

Core Types

N-Dimensional Sphere Types (in sphere_n module)

Integer Types (in ilds module)

Constants

Examples

See the examples directory for more usage examples.

Testing

Run the tests with:

cargo test

🛠️ Installation

📦 Cargo

📜 License

Licensed under either of

at your option.

🤝 Contribution

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.