Crates.io | randm |
lib.rs | randm |
version | 0.1.6 |
source | src |
created_at | 2024-11-19 20:35:32.900698 |
updated_at | 2024-11-20 12:18:30.852403 |
description | Small, fast, and efficient random generation crate |
homepage | |
repository | https://github.com/666rayen999/randm |
max_upload_size | |
id | 1453840 |
size | 6,445 |
Randm is a simple, fast, and efficient random generation crate for Rust. It aims to provide minimal overhead, quick random generation, and a small memory footprint, making it ideal for lightweight applications or performance-critical tasks.
cargo add randm
use randm::*;
fn main() {
let mut rng = Random::new(); // seed is set to time::now()
let random_number: u32 = rng.get();
println!("Generated random number: {}", random_number);
let mut rng = Random::seed(666); // or you can set seed manually
let random_number: f32 = rng.get();
println!("Generated random number: {}", random_number);
}
RandomT trait is already implemented for these types:
use randm::*;
#[Debug]
struct Vec2 {
x: f32,
y: f32,
}
impl RandomT for Vec2 {
fn random(r: &mut Random) -> Self {
Self {
x: r.get(),
y: r.get(),
}
}
}
fn main() {
let mut rng = Random::new();
let vec2: Vec2 = rng.get();
println!("Generated vec2: {:?}", vec2);
}
it uses the Xorshift algorithm with a period of 2^64-1
, meaning it will produce a repeated sequence only after 2^64-1
generations, or 18,446,744,073,709,551,615
unique values.
this is the algorithm used:
x ^= x << 7;
x ^= x >> 9;
macro feature to make it even simpler for custom structs
use randm::*;
#[Debug, Random]
struct Vec2 {
x: f32,
y: f32,
}
fn main() {
let mut rng = Random::new();
let vec2: Vec2 = rng.get();
println!("Generated vec2: {:?}", vec2);
}
Feel free to open issues or submit pull requests to improve the format.
This project is licensed under the MIT License.