How to generate random numbers in async rust?

Viewed 1491

The rng crate is neither Sync nor Send, so the rng::thread_rng can not cross the .await point. What is the simplest and beautiful way to generate random numbers in async rust?

Generating A lot of numbers beforehead and then use them is ugly.

1 Answers

Getting a new rng handle is cheap. A rng will be created once per thread (when used), and after that ThreadRng::default only uses LocalKey::with and Rc::clone. Do you need to keep the previous state so the number sequence is repeatable?

Related