# Alex Kladov - On Random Numbers (Highlights)

## Metadata
**Review**:: [readwise.io](https://readwise.io/bookreview/23272830)
**Source**:: #from/readwise #from/reader
**Zettel**:: #zettel/fleeting
**Status**:: #x
**Authors**:: [[Alex Kladov]]
**Full Title**:: On Random Numbers
**Category**:: #articles #readwise/articles
**Category Icon**:: 📰
**Document Tags**:: #rust
**URL**:: [matklad.github.io](https://matklad.github.io/2023/01/04/on-random-numbers.html)
**Host**:: [[matklad.github.io]]
**Highlighted**:: [[2023-09-02]]
**Created**:: [[2023-09-02]]
## Highlights
- In Rust, [`getrandom`](https://lib.rs/getrandom) crate provides a cross-platform wrapper for this functionality. ([View Highlight](https://read.readwise.io/read/01gpsfnvrgfxhxx5xkjdcekrwc)) ^456172522
- There are many different algorithms to do that. [`fastrand`](https://lib.rs/fastrand) crate implements something sufficiently close to the state of the art. ([View Highlight](https://read.readwise.io/read/01gpsfq5y7fv0v1t84tv5eaywr)) ^456172858
- Alternatively, a good-enough PRNG can be implemented in 9 lines of code: ([View Highlight](https://read.readwise.io/read/01gpsfr6wnsfka72k0v43nwk08)) ^456173188
#code
```rust
pub fn random_numbers(seed: u32) -> impl Iterator {
let mut random = seed;
std::iter::repeat_with(move || {
random ^= random << 13;
random ^= random >> 17;
random ^= random << 5;
random
})
}
```
- The best way to seed a PRNG is usually by using a fixed constant. If you absolutely need *some* amount of randomness in the seed, you can use the following hack ([View Highlight](https://read.readwise.io/read/01gpsfxe0t3jr5gj50atv3e4wd)) ^456175259
#code
```rust
pub fn random_seed() -> u64 {
std::hash::Hasher::finish(&std::hash::BuildHasher::build_hasher(
&std::collections::hash_map::RandomState::new(),
))
}
```
- Circling back to the beginning of the post, it is very important to distinguish between the two use-cases:
([View Highlight](https://read.readwise.io/read/01gpsg2ecj0a9j6jk288msxcst)) ^456177022
1. using unpredictable data for cryptography
2. using statistically uniform random data for stochastic algorithms---
cover: "https://readwise-assets.s3.amazonaws.com/static/images/article1.be68295a7e40.png"
---
# Alex Kladov - On Random Numbers (Highlights)

## Metadata
**Review**:: [readwise.io](https://readwise.io/bookreview/23272830)
**Source**:: #from/readwise #from/reader
**Zettel**:: #zettel/fleeting
**Status**:: #x
**Authors**:: [[Alex Kladov]]
**Full Title**:: On Random Numbers
**Category**:: #articles #readwise/articles
**Category Icon**:: 📰
**Document Tags**:: #rust
**URL**:: [matklad.github.io](https://matklad.github.io/2023/01/04/on-random-numbers.html)
**Host**:: [[matklad.github.io]]
**Highlighted**:: [[2023-09-02]]
**Created**:: [[2023-09-02]]
## Highlights
- In Rust, [`getrandom`](https://lib.rs/getrandom) crate provides a cross-platform wrapper for this functionality. ([View Highlight](https://read.readwise.io/read/01gpsfnvrgfxhxx5xkjdcekrwc)) ^456172522
- There are many different algorithms to do that. [`fastrand`](https://lib.rs/fastrand) crate implements something sufficiently close to the state of the art. ([View Highlight](https://read.readwise.io/read/01gpsfq5y7fv0v1t84tv5eaywr)) ^456172858
- Alternatively, a good-enough PRNG can be implemented in 9 lines of code: ([View Highlight](https://read.readwise.io/read/01gpsfr6wnsfka72k0v43nwk08)) ^456173188
#code
```rust
pub fn random_numbers(seed: u32) -> impl Iterator {
let mut random = seed;
std::iter::repeat_with(move || {
random ^= random << 13;
random ^= random >> 17;
random ^= random << 5;
random
})
}
```
- The best way to seed a PRNG is usually by using a fixed constant. If you absolutely need *some* amount of randomness in the seed, you can use the following hack ([View Highlight](https://read.readwise.io/read/01gpsfxe0t3jr5gj50atv3e4wd)) ^456175259
#code
```rust
pub fn random_seed() -> u64 {
std::hash::Hasher::finish(&std::hash::BuildHasher::build_hasher(
&std::collections::hash_map::RandomState::new(),
))
}
```
- Circling back to the beginning of the post, it is very important to distinguish between the two use-cases:
([View Highlight](https://read.readwise.io/read/01gpsg2ecj0a9j6jk288msxcst)) ^456177022
1. using unpredictable data for cryptography
2. using statistically uniform random data for stochastic algorithms
∎