Thanks to visit codestin.com
Credit goes to github.com

Skip to content

RylanYancey/just-rng

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

justrng

Non-cryptographic RNG for people who just want to generate random numbers for applications or procedural generation.

Seed generation

This crate uses web_time to seed the rng when compiling for wasm, and getrandom on other platforms. It will only do this once per thread, and will instantiate future seeds by updating a thread-local state.

Seeds can be generated manually with the seed module.

fn main() {
    let s1 = justrng::seed::from_local();
    let s2 = justrng::seed::from_system();
}

Vector support

Permutation and WyRand support generating and mixing vectors when the glam feature is enabled.

WyRand

The main RNG exported by this module is WyRand, complete with hash generation for all primitives, generating in a range, and shuffling slices.

fn main() {
    // instantiate your RNG with thread-local seed
    let mut rng = justrng::WyRand::new();

    // generate random numbers
    let mut n1 = rng.next::<u32>();
    let mut r1 = rng.next_in_range::<i64>(0..256);
    let mut f1 = rng.next_in_range::<f32>(-16.0..32.0);

    // shuffle slices
    let mut slice: Vec<i64> = vec![0, 1, 2, 3, 4, 5];
    rng.shuffle(&mut slice);
}

Permutation

An index-based rng that is lower quality than WyRand, but is very fast. Permutations are primarily used in procedural generation to hash vector coordinates. Unlike standard RNGs, Permutations do not update any state when they are used. It will produce the same value every time if the mix input (and seed) is the same.

use glam::IVec3;

fn main() {
    // instantiate the Permutation with thread-local seed
    let mut rng = justrng::Permutation::new();

    // mix vector coordinates
    let m1 = rng.mix(IVec3::new(-1, 245, 3));
    let m2 = rng.mix(IVec3::new(3, 99, 21));
    let m3 = rng.mix(IVec3::new(94, -21, 33));

    // mix the same vector twice, produces the same result.
    let vec = IVec3::new(27, -9, 41);
    let v1 = rng.mix(vec);
    let v2 = rng.mix(vec);
    assert_eq!(v1, v2);
}

About

Non-cryptographic RNG for applications and procedural generation (with WASM support)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages