Rust确定性均匀分布

huangapple go评论66阅读模式
英文:

Rust deterministic, uniform distribution

问题

我正在查看rand crate,并且不确定如何从一个分布中抽样,以便得到的样本既是均匀的又是确定性的。

确定性意味着在两次不同的程序调用中你会得到相同的数字。

我面临的主要问题是rand默认情况下不是确定性的。它有rand_pcg用于确定性,但我不知道如何使分布变得均匀。

英文:

I am looking at the rand crate and I am not sure how to sample from a distribution such that the resulting samples are uniform and deterministic.

Deterministic means that you get the same numbers on 2 distinct program invocations.

The main issue I am facing is that rand by default is not deterministic. Id does have rand_pcg for determinism, but then I don;t know how to make the distribution uniform.

答案1

得分: 3

rand crate支持你所需的一切。

均匀性

Rng trait上的gen*函数会实现这一点。在底层,它们实际上会延迟到Standard distribution,这只是大多数常见类型的均匀分布。

如果你希望提供自定义分布,只需使用Rng::sample*函数并提供你选择的分布。

确定性

你正在寻找SeedableRng。正如名称所示,实现此trait的rng应该能够在初始化时接受一个种子。请注意,实际上并不保证此trait会实现确定性(这取决于特定rng的实现),尽管在实践中,可种子的rng通常是确定性的。其中提供了一个这样的rng是StdRng,但如果你愿意,也可以实现自己的rng。

请注意,在StdRng的文档中,"确定性"和"可重现性"之间存在差异:

该算法是确定性的,但由于依赖于配置和可能在将来的库版本中更改,因此不应被视为可重现的。对于安全的可重现生成器,我们建议直接使用rand_chacha crate。

示例

use rand::{rngs::StdRng, Rng, SeedableRng};

fn main() {
    let mut rng = StdRng::seed_from_u64(69420);
    let x: i32 = rng.gen(); // 均匀分布

    // 这个值在你的机器上可能不同(不一定可重现)
    // 它也不能保证在不同的rust和rand版本之间相同
    // 但它应该在不同运行之间保持一致(确定性)
    assert_eq!(x, -817183091);
}
英文:

The rand crate supports everything you are asking for.

Uniformity

The gen* functions on the Rng trait do this. Under the hood, they actually defer to the Standard distribution, which is simply uniform distribution for most common types.

If you wish to provide a custom distribution, simply use Rng::sample* functions and provide a distribution of your choice.

Determinism

You are looking for SeedableRng. As the name implies, rngs that implement this trait should be able to accept a seed on initialisation. Note that determinism is not actually guaranteed by this trait (it depends on the implementation of the specific rng), although in practice seedable rngs are usually deterministic. One such rng provided is StdRng, but you can also implement your own rng if you so desire.

Note that there is a difference between "deterministic" and "reproducible", as outlined in StdRng's documentation:

> 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.

Example

use rand::{rngs::StdRng, Rng, SeedableRng};

fn main() {
    let mut rng = StdRng::seed_from_u64(69420);
    let x: i32 = rng.gen(); // uniform distribution

    // this value could be different on you machine (not necessarily reproducible)
    // and it's not guaranteed to be the same across rust&rand versions
    // but it should be consistent across runs (deterministic)
    assert_eq!(x, -817183091);
}

huangapple
  • 本文由 发表于 2023年6月5日 11:01:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76403274.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定