Generate random numbers using only standard library
Overview
This Rust crate provides various random number generation utilities, including:
- Random u8,u16,u32,u64,u128,usize generation.
- Non negative i8,i16,i32,i64,i128,isize generation
- Floating-point randomness (
f32andf64) in[0,1). - Random byte filling for buffers.
- Random range filling for slices.
- Random number generation from Range.
- High entropy random number generation by using separate thread
RandomState. - Fast but lower-entropy alternatives using thread local
RandomState.
Features
- Minimal supported Rust version 1.56. Entire 2021 Rust edition is supported.
- No external dependencies beyond standard Rust library.
- Thread-based randomness: Uses separate threads for improved entropy (
random_u64()). - Secure and fast implementations for generating random numbers.
- Uniform distribution ensured for floating-point values.
Functions returning random numbers
random_u128(), random_u64(), random_u32(), random_u16(), random_u8(), random_usize()
Generates a unsigned high entropy random number in a separate thread. These numbers are unpredictable - true randoms. Numbers have uniform distribution and pass chi square test with very good score.
fast_u128(), fast_u64(), fast_u32(), fast_u16(), fast_u8(), fast_usize()
Generates a lower entropy random number from current thread. This is much faster randomness and it still have uniform distribution. Its pseudo random number generator with 64bit internal state, next number can be predicted.
As expected chi square test score is several times worse than in random_ family. While chi square test is worse, it is still within the widely accepted limits for fast random number generators. Definitely these randoms are not good for generating cryptographic keys or for areas where hash resistance is required.
random_f64(), random_f32()
Generates a high entropy random floating point number in a separate thread from in [0, 1) range. These numbers have uniform distribution and pass chi square test.
fast_f32(), fast_f64()
Generates a lower entropy random floating point number in [0, 1).
Helper Functions
fill_bytes(), fill_numbers()
Fills slice with random numbers with provided generator.
gen_range(), fill_range()
Generate random number from specified range.
No copyright
This code is released under a "No Copyright" license.
You may use, modify, distribute, and contribute to this code without restriction. To the extent possible under law, the author(s) of this work waive all copyright and related rights.
Licensed under CC0-1.0 OR Unlicense.