#rng #hash

squirrel3-rs

Simple and reliable random number generator

3 releases

0.1.2 Oct 29, 2021
0.1.1 Oct 28, 2021
0.1.0 Oct 28, 2021

#2101 in Algorithms

MIT license

8KB
154 lines

squirrel3-rs

Simple and reliable; noise-based, random number generator.

Using; squirrel3 hashing function for the noise.

Usage

[dependencies]
squirrel3-rs = "0.1.2"
use squirrel3_rs::sq3;

fn main() {
    println!("{}", sq3::rand());
}

Examples

Basic:

println!("True or False => {}", sq3::bool());
println!("[0.0, 1.0) => {}", sq3::rand());
println!("[0, 100) => {}", sq3::u32(100));

Range:

println!("[2.14, 7.21) => {}", sq3::range_f32(2.14, 7.21));
println!("[-540, 2160) => {}", sq3::range_i32(-540, 2160));
// And more...

Array:

let mut fruits = vec!["apple", "banana", "peach", "orange", "strawberry"];
sq3::shuffle(&mut fruits);

println!("Shuffled: {:?}", fruits);
println!("I want to eat: {}", fruits[ sq3::usize(fruits.len()) ] );

Local State:

// Using manually created state, instead of thread_local.
let rng = sq3::Rng::new(2021 /*seed*/);
println!("True or False => {}", rng.bool());

Stateless Functions: ((No side effects))

// Original squirrel3
println!("[0, u32::MAX) => {}", sq3::squirrel3(3 /*position*/, 4 /*seed*/));
println!("[0, 1.0) => {}", sq3::normalized(3 /*position*/, 4 /*seed*/));

// Noise functions, returns a value between [0.0, 1.0)
println!("|x: 100| => {}", sq3::noise1d(100));
println!("|x: 100, y: 200| => {}", sq3::noise2d(100, 200));
println!("|x: 100, y: 200, z: 300| => {}", sq3::noise3d(100, 200, 300));

License

MIT

No runtime deps