Current version: 0.7.0
A library meant for fast, random number generation with quick compile time, and minimal dependencies.
use nanorand::{Rng, WyRand};
let mut rng = WyRand::new();
println!("Random number: {}", rng.generate::<u64>());
use nanorand::Rng;
let mut rng = nanorand::tls_rng();
println!("Random number: {}", rng.generate::<u64>());
use nanorand::{Rng, WyRand};
let mut rng = WyRand::new();
println!("Random number between 1 and 100: {}", rng.generate_range(1_u64..=100));
println!("Random number between -100 and 50: {}", rng.generate_range(-100_i64..=50));
use nanorand::{Rng, BufferedRng, WyRand};
let mut thingy = [0u8; 5];
let mut rng = BufferedRng::new(WyRand::new());
rng.fill(&mut thingy);
// As WyRand generates 8 bytes of output, and our target is only 5 bytes,
// 3 bytes will remain in the buffer.
assert_eq!(rng.buffered(), 3);
use nanorand::{Rng, WyRand};
let mut rng = WyRand::new();
let mut items = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
rng.shuffle(&mut items);
rand
- The standard rand crate is a complex beast. It contains unsafe code in the core implementations, and while it has much more options than we do, that's kind of the point. We're straight to the point, while rand is everything and the kitchen sink.fastrand
,oorandom
,random-fast-rng
, orrandomize
- These are all minimal, zero-dep implementations of the PCG family of RNGs (Pcg32 and Pcg64). While these are decent, they are much slower than wyrand (which beats the speed of these Pcg32 implementations while providing 64 random bits), and do not provide CSPRNGs.getrandom
- The getrandom crate just provides OS entropy sources. It is not meant for random number generation. In fact, we provide it as an optional entropy source.
RNG | nanorand type | Output Size | Cryptographically Secure | Speed1 | Notes | Original Implementation |
---|---|---|---|---|---|---|
wyrand | nanorand::WyRand , nanorand::tls::TlsWyRand |
64 bits (u64 ) |
🚫 | 14 GB/s | https://github.com/lemire/testingRNG/blob/master/source/wyrand.h | |
Pcg64 | nanorand::Pcg64 |
64 bits (u64 ) |
🚫 | 1.6 GB/s | https://github.com/rkern/pcg64 | |
ChaCha | nanorand::ChaCha |
512 bits ([u32; 16] ) |
✅ | 980 MB/s (ChaCha8), 749 MB/s (ChaCha12), 505 MB/s (ChaCha20) | https://cr.yp.to/chacha.html |
1. Speed benchmarked on an M1 Macbook Air
Listed in order of priority
- If the
getrandom
feature is enabled, thengetrandom::getrandom
will be called, and no other entropy sources will be used. - If the
rdseed
feature is enabled, and is running on an x86(-64) system with the RDSEED instruction, then we will attempt to source as much entropy as possible via ourrdseed_entropy
function - Linux and Android will attempt to use the
getrandom
syscall. - macOS and iOS (Darwin-based systems) will use Security.framework's
SecRandomCopyBytes
. - OpenBSD will attempt to use the
arc4random_buf
function. - Windows
- If we're targeting UWP, then the
BCryptGenRandom
is used with system-preferred RNG (BCRYPT_USE_SYSTEM_PREFERRED_RNG
). - Otherwise, we'll use
RtlGenRandom
.
- If we're targeting UWP, then the
alloc
(default) - Enables Rustalloc
lib features, such as a buffering Rng wrapper.std
(default) - Enables Ruststd
lib features, such as seeding from OS entropy sources. Requiresalloc
to be enabled.tls
(default) - Enables a thread-localWyRand
RNG (see below). Requiresstd
to be enabled.wyrand
(default) - Enable theWyRand
RNG.pcg64
(default) - Enable thePcg64
RNG.chacha
- Enable theChaCha
RNG. Requires Rust 1.47 or later.rdseed
- On x86 and x86-64 platforms, therdseed
intrinsic will be used when OS entropy isn't available.zeroize
- Implement the Zeroize trait for all RNGs.getrandom
- Use thegetrandom
crate as an entropy source. Works on most systems, optional due to the fact that it brings in more dependencies.
The minimum supported Rust version for the latest version of nanorand is Rust 1.56.0, released October 21st, 2021.
The zlib/libpng License
Copyright (c) 2022 Lucy [email protected]
This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
-
The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
-
Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
-
This notice may not be removed or altered from any source distribution.
I, @Absolucy, fully give permission for any of my code (including the entirety of this project, nanorand-rs), anywhere, no matter the license, to be used to train machine learning models intended to be used for general-purpose programming or code analysis.