如何使用 rand crate 为所有平台生成基于种子的可重现伪随机数?
How to produce reproducible pseudo-random numbers based on a seed for all platforms with the rand crate?
这是 . In 的后续内容,据说如果需要跨构建和架构的再现性,
then look at crates such as rand_chacha
, rand_pcg
, rand_xoshiro
documentation 中对 StdRng
的说明相同:
The algorithm is deterministic but should not be considered reproducible due to dependence on configuration and possible replacement in future library versions. For a secure reproducible generator, we recommend use of the rand_chacha crate directly.
我看过这些 crate,但我还没有找到如何将它们用于我的用例。我正在寻找一个小样本,展示这些板条箱中的一个如何用于生成一系列伪随机数,对于给定的种子,这些伪随机数总是相同的。
使用rand_chacha::ChaCha8Rng
的示例:
use rand_chacha::rand_core::SeedableRng;
use rand_core::RngCore;
use rand_chacha; // 0.3.0
fn main() {
let mut gen = rand_chacha::ChaCha8Rng::seed_from_u64(10);
let res: Vec<u64> = (0..100).map(|_| gen.next_u64()).collect();
println!("{:?}", res);
}
这是
then look at crates such as
rand_chacha
,rand_pcg
,rand_xoshiro
documentation 中对 StdRng
的说明相同:
The algorithm is deterministic but should not be considered reproducible due to dependence on configuration and possible replacement in future library versions. For a secure reproducible generator, we recommend use of the rand_chacha crate directly.
我看过这些 crate,但我还没有找到如何将它们用于我的用例。我正在寻找一个小样本,展示这些板条箱中的一个如何用于生成一系列伪随机数,对于给定的种子,这些伪随机数总是相同的。
使用rand_chacha::ChaCha8Rng
的示例:
use rand_chacha::rand_core::SeedableRng;
use rand_core::RngCore;
use rand_chacha; // 0.3.0
fn main() {
let mut gen = rand_chacha::ChaCha8Rng::seed_from_u64(10);
let res: Vec<u64> = (0..100).map(|_| gen.next_u64()).collect();
println!("{:?}", res);
}