Xtensor random :: randn返回的数字低于下限和上方的上限

发布于 2025-02-06 10:22:32 字数 489 浏览 3 评论 0 原文

我正在尝试生成随机数的形状{10},范围在-0.5和0.5之间,

#include <iostream>
#include <xtensor/xio.hpp>
#include <xtensor/xrandom.hpp>

int main() {
    xt::xarray<double> a = xt::random::randn<double>({10}, -0.5, 0.5);
    std::cout << a;
    return 0;
}

但它返回一个数组

{-0.432735, -0.573191, -0.269675, -1.435692, -0.418144, -0.607127,
 -0.350702, -0.913972, -0.494892,  0.027733}

,其中-0.573191较小,-1.435692大于范围。

I am trying to generate random numbers of shape {10} and range between -0.5 and 0.5

#include <iostream>
#include <xtensor/xio.hpp>
#include <xtensor/xrandom.hpp>

int main() {
    xt::xarray<double> a = xt::random::randn<double>({10}, -0.5, 0.5);
    std::cout << a;
    return 0;
}

but it's returning an array

{-0.432735, -0.573191, -0.269675, -1.435692, -0.418144, -0.607127,
 -0.350702, -0.913972, -0.494892,  0.027733}

where -0.573191 is smaller and -1.435692 is bigger than range

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浊酒尽余欢 2025-02-13 10:22:32

根据

template<class T, class S, class E = random::default_engine_type>
auto xt::random::randn(const S& shape, T mean = 0, T std_dev = 1,
                       E& engine = random::get_default_random_engine())

该函数从 您将平均值设置为 -0.5 ,标准偏差为 0.5 。这些不是下限和上限。


您可能需要使用 函数:

template<class T, class S, class E = random::default_engine_type>
auto xt::random::rand(const S& shape, T lower = 0, T upper = 1,
                      E& engine = random::get_default_random_engine())

这是从 - 因此,它不是同一件事,但它具有较低和上限。

According to the documentation for randn:

template<class T, class S, class E = random::default_engine_type>
auto xt::random::randn(const S& shape, T mean = 0, T std_dev = 1,
                       E& engine = random::get_default_random_engine())

The function draws numbers from std::normal_distribution and you've set the mean value to be -0.5 and the standard deviation to be 0.5. These are not lower and upper bounds.


You may want to use the rand function instead:

template<class T, class S, class E = random::default_engine_type>
auto xt::random::rand(const S& shape, T lower = 0, T upper = 1,
                      E& engine = random::get_default_random_engine())

This draws numbers from std::uniform_real_distribution - so, it's not the same thing, but it has a lower and an upper bound.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文