Open utterances-bot opened 1 year ago
That’s what I use to:
use std::collections::hash_map::RandomState;
use std::hash::{BuildHasher, Hasher};
let random_value = RandomState::new().build_hasher().finish() as usize;
println!("Random: {}", random_value);
If you're unhappy with the number of dependencies rand
pulls in, you can always use getrandom
directly, although it does have dependencies on cfg-if
and libc
as well (on *IX anyway)
It's fascinating to explore the complexities of generating random numbers in Rust without relying on external tools. The explanation clarifies the challenges posed by the deterministic nature of computers, leading to pseudorandomness. The breakdown of using 'rand' and 'fastrand' crates, along with alternatives like nanoseconds and /dev/urandom, offers practical insights into achieving randomness with minimal dependencies. The discussion around system calls like getrandom() and even leveraging C library functions adds an extra layer of depth to the exploration. Overall, a thought-provoking journey into the intricacies of randomness in Rust!
Orhun's Blog
https://blog.orhun.dev/zero-deps-random-in-rust/