gpt4 book ai didi

random - 从标准正态分布生成随机 float 并乘以另一个 float

转载 作者:行者123 更新时间:2023-12-03 11:31:24 24 4
gpt4 key购买 nike

尝试从标准正态分布生成随机数。需要将该值乘以 0.1 以获得我正在寻找的数字范围。我尝试使用 rand_dist 中的文档,您可以在此处找到:https://docs.rs/rand_distr/0.3.0/rand_distr/struct.StandardNormal.html

我的 Cargo.toml 如下:

[package]
name = "test_rng"
version = "0.1.0"
authors = ["Jack"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rand = "0.7.3"
rand_distr = "0.3.0"

开始的 Rust 代码是上面 rand_dist 文档中提供的示例:

use rand::prelude::*;
use rand_distr::StandardNormal;

fn main() {
let val: f64 = thread_rng().sample(StandardNormal);
println!("{}", val);
}

当我运行它时,它按预期工作,输出为:

C:\Users\Jack\Desktop\projects\software\rust\test_rng>cargo run
Compiling test_rng v0.1.0 (C:\Users\Jack\Desktop\projects\software\rust\test_rng)
Finished dev [unoptimized + debuginfo] target(s) in 2.11s
Running `target\debug\test_rng.exe`
0.48398855288705356

C:\Users\Jack\Desktop\projects\software\rust\test_rng>

这是我遇到问题的地方,当我尝试在以下代码中将数字乘以 0.1 时,我得到了结果错误:

fn main() {
let val: f64 = 0.1 * thread_rng().sample(StandardNormal);
println!("{}", val);
}
C:\Users\Jack\Desktop\projects\software\rust\test_rng>cargo run
Compiling test_rng v0.1.0 (C:\Users\Jack\Desktop\projects\software\rust\test_rng)
error[E0284]: type annotations needed: cannot satisfy `<f64 as std::ops::Mul<_>>::Output == f64`
--> src\main.rs:5:24
|
5 | let val: f64 = 0.1 * thread_rng().sample(StandardNormal);
| ^ cannot satisfy `<f64 as std::ops::Mul<_>>::Output == f64`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0284`.
error: could not compile `test_rng`.

To learn more, run the command again with --verbose.

C:\Users\Jack\Desktop\projects\software\rust\test_rng>

我试图将 0.1 更改为 0.1_f64,但出现了同样的错误。我尝试使用 as f64 将随机数转换为 f64(它应该已经是),但结果如下:

fn main() {
let val: f64 = 0.1 * thread_rng().sample(StandardNormal) as f64;
println!("{}", val);
}
C:\Users\Jack\Desktop\projects\software\rust\test_rng>cargo run
Compiling test_rng v0.1.0 (C:\Users\Jack\Desktop\projects\software\rust\test_rng)
error[E0282]: type annotations needed
--> src\main.rs:5:39
|
5 | let val: f64 = 0.1 * thread_rng().sample(StandardNormal) as f64;
| ^^^^^^ cannot infer type for type parameter `T` declared on the associated function `sample`
|
= note: type must be known at this point
help: consider specifying the type arguments in the method call
|
5 | let val: f64 = 0.1 * thread_rng().sample::<T, D>(StandardNormal) as f64;
| ^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0282`.
error: could not compile `test_rng`.

To learn more, run the command again with --verbose.

C:\Users\Jack\Desktop\projects\software\rust\test_rng>

认为这是一个优先级问题,所以我尝试将后半部分括在括号中,但得到了同样的错误。

我可以通过使变量可变并将该行分成两个操作来使其工作,如下所示:

fn main() {
let mut val: f64 = thread_rng().sample(StandardNormal);
val *= 0.1;
println!("{}", val);
}
C:\Users\Jack\Desktop\projects\software\rust\test_rng>cargo run
Compiling test_rng v0.1.0 (C:\Users\Jack\Desktop\projects\software\rust\test_rng)
Finished dev [unoptimized + debuginfo] target(s) in 1.62s
Running `target\debug\test_rng.exe`
-0.034993448117065

C:\Users\Jack\Desktop\projects\software\rust\test_rng>

知道 f64 与随机数输出的乘积是怎么回事吗?

最佳答案

您可以使用以下内容:

fn main() {
let val: f64 = 0.1 * thread_rng().sample::<f64,_>(StandardNormal);
println!("{}", val);
}

这显式强制示例函数返回 f64。可能发生的情况是 rust 类型推断没有意识到 RHS 需要是 f64,尽管我不确定确切原因。

编辑:我认为这里的部分责任归咎于 sample 的定义,因为它使用了不受限制的类型参数。一个 MVE 是这样的:

pub trait Marker{}

impl Marker for f64{}
impl Marker for f32{}

fn does_not_work<T>() -> T{
unimplemented!()
}

fn does_work<T: Marker>() -> T{
unimplemented!()
}

fn main() {
let val: f64 = 0.1 * does_work();
let val: f64 = 0.1 * does_not_work();
}

编译器无法为 does_not_work 推断类型,这在某种程度上是可以理解的,b/c 了解可以与 f64 相乘的每种可能类型意味着什么?然而,如果我们将事物限制为仅具有特征的某些类型,则可能的类型列表将变得有限,并且类型推断再次起作用。

关于random - 从标准正态分布生成随机 float 并乘以另一个 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64724454/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com