orhun / personal-blog

The source of my blog ✍🏼
https://blog.orhun.dev
27 stars 3 forks source link

https://blog.orhun.dev/zero-deps-random-in-rust/ #3

Open utterances-bot opened 1 year ago

utterances-bot commented 1 year ago

Orhun's Blog

https://blog.orhun.dev/zero-deps-random-in-rust/

matklad commented 1 year ago

That’s what I use to:

https://github.com/matklad/config/blob/b8ea0aad0f86d4575651a390a3c7aefb63229774/templates/snippets/src/lib.rs#L28L42

badboy commented 1 year ago
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);
tarcieri commented 1 year ago

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)

techbeamers commented 5 months ago

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!