#random #numbers #warnings

no-std tiny-rng

Tiny RNG, a minimal random number generator

3 releases (breaking)

0.3.0 Feb 9, 2025
0.2.0 Apr 5, 2021
0.1.0 Jun 4, 2019

#2557 in Algorithms

Download history 10/week @ 2025-10-02 293/week @ 2025-10-09 306/week @ 2025-10-16 19/week @ 2025-10-23 8/week @ 2025-10-30 40/week @ 2025-11-06 1/week @ 2025-11-13 23/week @ 2025-11-20 2/week @ 2025-12-18 54/week @ 2026-01-01

54 downloads per month
Used in 2 crates

CC0 license

13KB
168 lines

Tiny RNG, a minimal random number generator

Warning: Not cryptographically secure.

Proven mathematical methods are applied to obtain unbiased samples. Specifically, rejection sampling is applied to obtain samples from the uniform distribution on an integer range and Fisher–Yates shuffle is applied to obtain a random permutation from the uniform distribution on the set of all permutations.

use tiny_rng::{Rng, Rand};

let mut rng = Rng::from_seed(0);

// Throw a dice:
println!("{}", rng.rand_range_u32(1, 7));

// Choose a random color:
let colors = ["red", "green", "blue"];
println!("{}", rng.choice(&colors));

// Shuffle an array:
let mut a = [1, 2, 3, 4];
rng.shuffle(&mut a);
println!("{:?}", a);

Tiny RNG, a minimal random number generator

Warning: Not cryptographically secure.

Examples:

use tiny_rng::{Rng, Rand};

fn main() {
    let mut rng = Rng::from_seed(0);
  
    println!("A u32 random number: 0x{:08x}", rng.rand_u32());
    println!("Throw a dice: {}", rng.rand_range_u32(1, 6));

    let a: Vec<u32> = rng.iter(Rand::rand_u32).take(4).collect();
    println!("An array of u32 random numbers: {:08x?}", a);
    
    let a: Vec<u32> = rng.iter(|rng| rng.rand_range_u32(1, 6))
        .take(4).collect();
    println!("An array of dice samples: {:?}", a);

    let mut a: Vec<u32> = (0..10).collect();
    rng.shuffle(&mut a);
    println!("A shuffled array: {:?}", a);
    
    let mut a: [u8;4] = [0, 0, 0, 0];
    rng.fill(&mut a);
    println!("Random bytes: {:?}", a);
}

No runtime deps

Features